A-A+

jquery对checkbox,radio,select操作方法总结

2016年01月11日 前端设计 暂无评论 阅读 6 views 次

js中checkbox,radio,select三种属性与input操作不一样,许多新手朋友对于checkbox,radio,select操作不是很了解用起来也不方便了,下面我们整理了jquery对checkbox,radio,select操作方法总结,希望例子能够帮助到各位。

jquery的checkbox,radio,和select是jquery操作的一个难点和重点,很多前端新手对其了解不是很透彻。时间久了不用,我在写的时候有时也难免对某些操作支支吾吾,记不清楚,现在,对其做一些简单的总结!

1、checkbox日常jquery操作。

现在我们以下面的html为例进行checkbox的操作。

  1. <input id="checkAll" type="checkbox" />全选  
  2.         <input name="subBox" type="checkbox" />项1  
  3.         <input name="subBox" type="checkbox" />项2  
  4.         <input name="subBox" type="checkbox" />项3  
  5.         <input name="subBox" type="checkbox" />项4  
  6. 全选和全部选代码:  
  7. <script type="text/javascript">  
  8.         $(function() {  
  9.            $("#checkAll").click(function() {  
  10.                 $('input[name="subBox"]').attr("checked",this.checked);   
  11.             });  
  12.             var $subBox = $("input[name='subBox']");  
  13.             $subBox.click(function(){  
  14.                 $("#checkAll").attr("checked",$subBox.length == $("input[name='subBox']:checked").length ? true : false);  
  15.             });  
  16.         });  
  17.     </script>  

checkbox属性:

  1. var val = $("#checkAll").val();// 获取指定id的复选框的值    
  2. var isSelected = $("#checkAll").attr("checked"); // 判断id=checkAll的那个复选框是否处于选中状态,选中则isSelected=true;否则isSelected=false;    
  3. $("#checkAll").attr("checked"true);// or    
  4. $("#checkAll").attr("checked", 'checked');// 将id=checkbox_id3的那个复选框选中,即打勾    
  5. $("#checkAll").attr("checked"false);// or    
  6. $("#checkAll").attr("checked"'');// 将id=checkbox_id3的那个复选框不选中,即不打勾    
  7. $("input[name=subBox][value=3]").attr("checked", 'checked');// 将name=subBox, value=3 的那个复选框选中,即打勾    
  8. $("input[name=subBox][value=3]").attr("checked"'');// 将name=subBox, value=3 的那个复选框不选中,即不打勾    
  9. $("input[type=checkbox][name=subBox]").get(2).checked = true;// 设置index = 2,即第三项为选中状态    
  10. $("input[type=checkbox]:checked").each(function(){ //由于复选框一般选中的是多个,所以可以循环输出选中的值    
  11.     alert($(this).val());    
  12. });  

2、radio的jquery日常操作及属性

我们仍然以下面的html为例:

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

radio操作如下:

  1. $("input[name=radio]:eq(0)").attr("checked",'checked'); //这样就是第一个选中咯。  
  2.   //jquery中,radio的选中与否和checkbox是一样的。  
  3. $("#radio1").attr("checked","checked");  
  4. $("#radio1").removeAttr("checked");  
  5. $("input[type='radio'][name='radio']:checked").length == 0 ? "没有任何单选框被选中" : "已经有选中";    
  6. $('input[type="radio"][name="radio"]:checked').val(); // 获取一组radio被选中项的值    
  7. $("input[type='radio'][name='radio'][value='2']").attr("checked""checked");// 设置value = 2的一项为选中    
  8. $("#radio2").attr("checked""checked"); // 设置id=radio2的一项为选中    
  9. $("input[type='radio'][name='radio']").get(1).checked = true// 设置index = 1,即第二项为当前选中    
  10. var isChecked = $("#radio2").attr("checked");// id=radio2的一项处于选中状态则isChecked = true, 否则isChecked = false;    
  11. var isChecked = $("input[type='radio'][name='radio'][value='2']").attr("checked");// value=2的一项处于选中状态则isChecked = true, 否则isChecked = false;   

3、select下拉框的日常jquery操作

select操作相比checkbox和radio要相对麻烦一些,我们仍然以下面的html为例来说明:

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

看select的如下属性:

  1. $("#select_id").change(function(){                         // 1.为Select添加事件,当选择其中一项时触发     
  2.         //code...    
  3.     });    
  4.     var checkValue = $("#select_id").val();                    // 2.获取Select选中项的Value    
  5.     var checkText = $("#select_id :selected").text();          // 3.获取Select选中项的Text     
  6.     var checkIndex = $("#select_id").attr("selectedIndex");    // 4.获取Select选中项的索引值,或者:$("#select_id").get(0).selectedIndex;    
  7.     var maxIndex = $("#select_id :last").attr("index");        // 5.获取Select最大的索引值,或者:$("#select_id :last").get(0).index;    
  8.     /**  
  9.      * jQuery设置Select的选中项  
  10.      */    
  11.     $("#select_id").get(0).selectedIndex = 1;                  // 1.设置Select索引值为1的项选中    
  12.     $("#select_id").val(4);                                    // 2.设置Select的Value值为4的项选中    
  13.     /**  
  14.      * jQuery添加/删除Select的Option项  
  15.      */    
  16.     $("#select_id").append("<option value='新增'>新增option</option>");    // 1.为Select追加一个Option(下拉项)     
  17.     $("#select_id").prepend("<option value='请选择'>请选择</option>");   // 2.为Select插入一个Option(第一个位置)    
  18.     $("#select_id").get(0).remove(1);                                      // 3.删除Select中索引值为1的Option(第二个)    
  19.     $("#select_id :last").remove();                                        // 4.删除Select中索引值最大Option(最后一个)     
  20.     $("#select_id [value='3']").remove();                                  // 5.删除Select中Value='3'的Option     
  21.     $("#select_id").empty();              
  22.    $("#select_id").find("option:selected").text(); // 获取select 选中的 text :  
  23.    $("#select_id").val(); // 获取select选中的 value:  
  24.      $("#select_id").get(0).selectedIndex; // 获取select选中的索引:  
  25.      $("#select_id").get(0).selectedIndex=index;//index为索引值  
  26.  //设置select 选中的value:  
  27.     $("#select_id").attr("value","Normal“); 
  28.     $("#select_id").val("Normal"); 
  29.     $("#select_id").get(0).value = value; 
  30.  //设置select 选中的text,通常可以在select回填中使用 
  31. var numId=33 //设置text==33的选中! 
  32. var count=$("#select_id  option").length; 
  33.   for(var i=0;i<count;i++)   
  34.      {           if($("#select_id").get(0).options[i].text == numId)   
  35.         {   
  36.             $("#select_id").get(0).options[i].selected = true;    
  37.             break;    
  38.         }    
  39.     }  

通过上面的总结,应该对jquery的checkbox,radio和select有了一定的了解了吧,温故而知新,用多了就会变的熟练起来,即使有时候忘记了,也可以来翻一翻!

标签:

给我留言