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()  ==   0 return   " 下拉框中无选项 "   
        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/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值