jQuery操作radio、checkbox、select总结

1、radio:单选框

       HTML代码:

<input type="radio" name="radio" id="radio1" value="1" />1     
    <input type="radio" name="radio" id="radio1" value="1" />1  
    <input type="radio" name="radio" id="radio2" value="2" />2  
    <input type="radio" name="radio" id="radio3" value="3" />3  
    <input type="radio" name="radio" id="radio4" value="4" />4  

 

        js操作代码:

 

2、checkbox:复选框

       HTML代码:

    <input type="checkbox" name="checkbox" id="checkAll" />全选/取消全选  
    <input type="checkbox" name="checkbox" id="checkbox_id1" value="1" />1  
    <input type="checkbox" name="checkbox" id="checkbox_id2" value="2" />2  
    <input type="checkbox" name="checkbox" id="checkbox_id3" value="3" />3  
    <input type="checkbox" name="checkbox" id="checkbox_id4" value="4" />4  
    <input type="checkbox" name="checkbox" id="checkbox_id5" value="5" />5  

 

       js操作代码:

 

  3、select:下拉框

       HTML代码:

<select name="select" id="select_id" style="width: 100px;">  
    <option value="1">11</option>  
    <option value="2">22</option>  
    <option value="3">33</option>  
    <option value="4">44</option>  
    <option value="5">55</option>  
    <option value="6">66</option>  
</select> 

 

       js操作代码:

      /**   
  1.  * jQuery获取select的各种值   
  2.  */     
  3. jQuery("#select_id").change(function(){                         // 1.为Select添加事件,当选择其中一项时触发     
  4.     //code...     
  5. });     
  6. var checkValue = jQuery("#select_id").val();                    // 2.获取Select选中项的Value    
  7. var checkText = jQuery("#select_id :selected").text();          // 3.获取Select选中项的Text     
  8. var checkIndex = jQuery("#select_id").attr("selectedIndex");    // 4.获取Select选中项的索引值,或者:jQuery("#select_id").get(0).selectedIndex;    
  9. var maxIndex = jQuery("#select_id :last").attr("index");        // 5.获取Select最大的索引值,或者:jQuery("#select_id :last").get(0).index;    
  10. /**   
  11.  * jQuery设置Select的选中项   
  12.  */     
  13. jQuery("#select_id").get(0).selectedIndex = 1;                  // 1.设置Select索引值为1的项选中    
  14. jQuery("#select_id").val(4);                                    // 2.设置Select的Value值为4的项选中    
  15. /**   
  16.  * jQuery添加/删除Select的Option项   
  17.  */     
  18. jQuery("#select_id").append("<option value='新增'>新增option</option>");    // 1.为Select追加一个Option(下拉项)     
  19. jQuery("#select_id").prepend("<option value='请选择'>请选择</option>");   // 2.为Select插入一个Option(第一个位置)    
  20. jQuery("#select_id").get(0).remove(1);                                      // 3.删除Select中索引值为1的Option(第二个)    
  21. jQuery("#select_id :last").remove();                                        // 4.删除Select中索引值最大Option(最后一个)     
  22. jQuery("#select_id [value='3']").remove();                                  // 5.删除Select中Value='3'的Option     
  23. jQuery("#select_id").empty();                                               // 6.清空下拉列表    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值