Jquery 對select 操作

1.獲取列表項中候選項的數目。

2.獲得選中項的索引值。

3.獲得當前選中項的值

4.設定選擇值

5.設定選擇項

...

   //得到select項的個數   
   jQuery.fn.size = function(){   
       return jQuery(this).get(0).options.length;   
   }   
   
   //獲得選中項的索引   
   jQuery.fn.getSelectedIndex = function(){   
       return jQuery(this).get(0).selectedIndex;   
   }   
  
  //獲得當前選中項的文本   
  jQuery.fn.getSelectedText = function(){   
      if(this.size() == 0return "下拉框中無選項";   
      else{   
          var index = this.getSelectedIndex();         
          return jQuery(this).get(0).options[index].text;   
      }   
  }   
  
  //獲得當前選中項的值   
  jQuery.fn.getSelectedValue = function(){   
      if(this.size() == 0)    
          return "下拉框中無選中值";   
         
      else 
          return jQuery(this).val();   
  }   
  
  //設置select中值爲value的項爲選中   
  jQuery.fn.setSelectedValue = function(value){   
      jQuery(this).get(0).value = value;   
  }   
  
  //設置select中文本爲text的第一項被選中   
  jQuery.fn.setSelectedText = function(text)   
  {   
      var isExist = false;   
      var count = this.size();   
      for(var i=0;i<count;i++)   
      {   
          if(jQuery(this).get(0).options[i].text == text)   
          {   
              jQuery(this).get(0).options[i].selected = true;   
              isExist = true;   
              break;   
          }   
      }   
      if(!isExist)   
      {   
          alert("下拉框中不存在該項");   
      }   
  }   
  //設置選中指定索引項   
  jQuery.fn.setSelectedIndex = function(index)   
  {   
      var count = this.size();       
      if(index >= count || index < 0)   
      {   
          alert("選中項索引超出範圍");   
      }   
      else 
      {   
          jQuery(this).get(0).selectedIndex = index;   
      }   
  }   
  //判斷select項中是否存在值爲value的項   
  jQuery.fn.isExistItem = function(value)   
  {   
      var isExist = false;   
      var count = this.size();   
      for(var i=0;i<count;i++)   
      {   
          if(jQuery(this).get(0).options[i].value == value)   
          {   
              isExist = true;   
              break;   
          }   
      }   
      return isExist;   
  }   
  //向select中添加一項,顯示內容爲text,值爲value,如果該項值已存在,則提示   
  jQuery.fn.addOption = function(text,value)   
  {   
      if(this.isExistItem(value))   
      {   
          alert("待添加項的值已存在");   
      }   
      else 
      {   
          jQuery(this).get(0).options.add(new Option(text,value));   
      }   
  }   
  //刪除select中值爲value的項,如果該項不存在,則提示   
  jQuery.fn.removeItem = function(value)   
  {       
      if(this.isExistItem(value))   
      {   
          var count = this.size();           
          for(var i=0;i<count;i++)   
         {   
             if(jQuery(this).get(0).options[i].value == value)   
             {   
                 jQuery(this).get(0).remove(i);   
                 break;   
             }   
         }           
     }   
     else 
     {   
         alert("待刪除的項不存在!");   
     }   
 }   
 //刪除select中指定索引的項   
 jQuery.fn.removeIndex = function(index)   
 {   
     var count = this.size();   
     if(index >= count || index < 0)   
     {   
         alert("待刪除項索引超出範圍");   
     }   
     else 
     {   
         jQuery(this).get(0).remove(index);   
     }   
 }   
 //刪除select中選定的項   
 jQuery.fn.removeSelected = function()   
 {   
     var index = this.getSelectedIndex();   
     this.removeIndex(index);   
 }   
 //清除select中的所有項   
 jQuery.fn.clearAll = function()   
 {   
     jQuery(this).get(0).options.length = 0;   
 
 
 
----------------------------------------
 
jQuery獲取客戶端控件select
一、 獲取select中選擇的text與value相關的值
獲取select選擇的Text : var checkText=$("#slc1").find("option:selected").text();
獲取select選擇的value:var checkValue=$("#slc1").val();
獲取select選擇的索引值: var checkIndex=$("#slc1 ").get(0).selectedIndex;
獲取select最大的索引值: var maxIndex=$("#slc1 option:last").attr("index");
二、  設置select選擇的Text和Value
設置select索引值爲1的項選中:$("#slc1 ").get(0).selectedIndex=1;  
設置select的value值爲4的項選中: $("#slc1 ").val(4);  
設置select的Text值爲JQuery的選中:
$("#slc1 option[text='jQuery']").attr("selected", true);
PS:特別要注意一下第三項的使用哦。看看JQuery的選擇器功能是如此地強大呀!
三、 添加刪除option項
爲select追加一個Option(下拉項)
$("#slc2").append("<option value='"+i+"'>"+i+"</option>");
爲select插入一個option(第一個位置)
$("#slc2").prepend("<option value='0'>請選擇</option>");
PS: prepend 這是向所有匹配元素內部的開始處插入內容的最佳方式。
刪除select中索引值最大option(最後一個)
$("#slc2 option:last").remove();
刪除select中索引值爲0的option(第一個)
$("#slc2 option[index='0']").remove();
刪除select中value=’3’的option
$("#slc2 option[value='3']").remove();
刪除select中text=’4’的option
$("#slc2 option[text='3']").remove();

 

---------------------------------------------------------

 

以下來自夢三秋http://www.skygq.com/2010/12/24/jquery-process-select-demo/


<script type="text/javascript">
 $(document).ready(function(){
  
  //添加“江蘇”到下拉框的最後一位
  $('#add_to_last').click(function(){
     $('#select').append('<OPTION value="江蘇">江蘇</OPTION>');
  });
  
  //添加“安徽”到下拉框的第一位
  $('#add_to_first').click(function(){ 
   $('#select').prepend('<OPTION value="安徽">安徽</OPTION>'); 
  });

  //獲取當前的selectIndex(當前選中的下拉菜單的項目的索引)
  $('#get_select_index').click(function(){
   alert($('#select option:selected').attr("index"));
  });
  
  //移除下拉菜單最後一個項目
  $('#remove_last_option').click(function(){
   $('#select option:last').remove();
  });
  
  //移除除了第一個之外的所有選項
  $('#remove_all_option_except_first').click(function(){
   $('#select option').not(':first').remove();
  });
  
  //獲取下拉菜單最大索引值
  $('#get_max_index').click(function(){
   var maxIndex=$("#select option:last").attr("index");
   alert(maxIndex);
  });
  
 });
</script>

<SELECT id="select" style="width:100px"> <OPTION selected>中國省份</OPTION></SELECT>  <p/>
<button id="add_to_last">添加江蘇到最後一位</button><br/>
<button id="add_to_first">添加安徽到第一位</button><br/>
<button id="get_select_index">獲取當前的selectIndex</button><br/>
<button id="remove_last_option">移除下拉菜單最後一個項目</button><br/>
<button id="remove_all_option_except_first">移除除了第一個之外的所有選項</button><br/>
<button id="get_max_index">獲取下拉菜單最大索引值</button><br/>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章