jQuery操做radio、checkbox、select總結

 

一、radio:單選框javascript

       HTML代碼:html

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   
    <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操做代碼:java

Js代碼 
  1. jQuery("input[type='radio'][name='radio']:checked").length == 0 ? "沒有任何單選框被選中" : "已經有選中";   
  2. jQuery('input[type="radio"][name="radio"]:checked').val(); // 獲取一組radio被選中項的值   
  3. jQuery("input[type='radio'][name='radio'][value='2']").attr("checked", "checked");// 設置value = 2的一項爲選中   
  4. jQuery("#radio2").attr("checked", "checked"); // 設置id=radio2的一項爲選中   
  5. jQuery("input[type='radio'][name='radio']").get(1).checked = true; // 設置index = 1,即第二項爲當前選中   
  6. var isChecked = jQuery("#radio2").attr("checked");// id=radio2的一項處於選中狀態則isChecked = true, 不然isChecked = false;   
  7. var isChecked = jQuery("input[type='radio'][name='radio'][value='2']").attr("checked");// value=2的一項處於選中狀態則isChecked = true, 不然isChecked = false;   
    jQuery("input[type='radio'][name='radio']:checked").length == 0 ? "沒有任何單選框被選中" : "已經有選中";  
    jQuery('input[type="radio"][name="radio"]:checked').val(); // 獲取一組radio被選中項的值  
    jQuery("input[type='radio'][name='radio'][value='2']").attr("checked", "checked");// 設置value = 2的一項爲選中  
    jQuery("#radio2").attr("checked", "checked"); // 設置id=radio2的一項爲選中  
    jQuery("input[type='radio'][name='radio']").get(1).checked = true; // 設置index = 1,即第二項爲當前選中  
    var isChecked = jQuery("#radio2").attr("checked");// id=radio2的一項處於選中狀態則isChecked = true, 不然isChecked = false;  
    var isChecked = jQuery("input[type='radio'][name='radio'][value='2']").attr("checked");// value=2的一項處於選中狀態則isChecked = true, 不然isChecked = false;  

 

二、checkbox:複選框app

       HTML代碼:this

Html代碼 
  1. <input type="checkbox" name="checkbox" id="checkAll" />全選/取消全選   
  2. <input type="checkbox" name="checkbox" id="checkbox_id1" value="1" />1   
  3. <input type="checkbox" name="checkbox" id="checkbox_id2" value="2" />2   
  4. <input type="checkbox" name="checkbox" id="checkbox_id3" value="3" />3   
  5. <input type="checkbox" name="checkbox" id="checkbox_id4" value="4" />4   
  6. <input type="checkbox" name="checkbox" id="checkbox_id5" value="5" />5   
    <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操做代碼:spa

Js代碼 
  1. var val = jQuery("#checkbox_id1").val();// 獲取指定id的複選框的值   
  2. var isSelected = jQuery("#checkbox_id3").attr("checked"); // 判斷id=checkbox_id3的那個複選框是否處於選中狀態,選中則isSelected=true;不然isSelected=false;   
  3. jQuery("#checkbox_id3").attr("checked", true);// or   
  4. jQuery("#checkbox_id3").attr("checked", 'checked');// 將id=checkbox_id3的那個複選框選中,即打勾   
  5. jQuery("#checkbox_id3").attr("checked", false);// or   
  6. jQuery("#checkbox_id3").attr("checked", '');// 將id=checkbox_id3的那個複選框不選中,即不打勾   
  7. jQuery("input[name=checkbox][value=3]").attr("checked", 'checked');// 將name=checkbox, value=3 的那個複選框選中,即打勾   
  8. jQuery("input[name=checkbox][value=3]").attr("checked", '');// 將name=checkbox, value=3 的那個複選框不選中,即不打勾   
  9. jQuery("input[type=checkbox][name=checkbox]").get(2).checked = true;// 設置index = 2,即第三項爲選中狀態   
  10. jQuery("input[type=checkbox]:checked").each(function(){ //因爲複選框通常選中的是多個,因此能夠循環輸出選中的值   
  11.     alert(jQuery(this).val());   
  12. });   
  13. // 全選/取消全選   
  14. jQuery(function() {   
  15.     jQuery("#checkAll").click(function(){   
  16.             if(jQuery(this).attr("checked") == true){// 全選   
  17.                 jQuery("input[type=checkbox][name=checkbox]").each(function(){   
  18.                         jQuery(this).attr("checked", true);   
  19.                     });   
  20.             } else {// 取消全選   
  21.                 jQuery("input[type=checkbox][name=checkbox]").each(function(){   
  22.                     jQuery(this).attr("checked", false);   
  23.                 });   
  24.             }   
  25.         });   
  26. });   
    var val = jQuery("#checkbox_id1").val();// 獲取指定id的複選框的值  
    var isSelected = jQuery("#checkbox_id3").attr("checked"); // 判斷id=checkbox_id3的那個複選框是否處於選中狀態,選中則isSelected=true;不然isSelected=false;  
    jQuery("#checkbox_id3").attr("checked", true);// or  
    jQuery("#checkbox_id3").attr("checked", 'checked');// 將id=checkbox_id3的那個複選框選中,即打勾  
    jQuery("#checkbox_id3").attr("checked", false);// or  
    jQuery("#checkbox_id3").attr("checked", '');// 將id=checkbox_id3的那個複選框不選中,即不打勾  
    jQuery("input[name=checkbox][value=3]").attr("checked", 'checked');// 將name=checkbox, value=3 的那個複選框選中,即打勾  
    jQuery("input[name=checkbox][value=3]").attr("checked", '');// 將name=checkbox, value=3 的那個複選框不選中,即不打勾  
    jQuery("input[type=checkbox][name=checkbox]").get(2).checked = true;// 設置index = 2,即第三項爲選中狀態  
    jQuery("input[type=checkbox]:checked").each(function(){ //因爲複選框通常選中的是多個,因此能夠循環輸出選中的值  
        alert(jQuery(this).val());  
    });  
    // 全選/取消全選  
    jQuery(function() {  
        jQuery("#checkAll").click(function(){  
                if(jQuery(this).attr("checked") == true){// 全選  
                    jQuery("input[type=checkbox][name=checkbox]").each(function(){  
                            jQuery(this).attr("checked", true);  
                        });  
                } else {// 取消全選  
                    jQuery("input[type=checkbox][name=checkbox]").each(function(){  
                        jQuery(this).attr("checked", false);  
                    });  
                }  
            });  
    });  

 

  三、select:下拉框code

       HTML代碼:xml

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 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操做代碼:htm

Js代碼 
  1. /**
  2. * jQuery獲取select的各類值
  3. */   
  4. jQuery("#select_id").change(function(){                         // 1.爲Select添加事件,當選擇其中一項時觸發    
  5.     //code...   
  6. });   
  7. var checkValue = jQuery("#select_id").val();                    // 2.獲取Select選中項的Value   
  8. var checkText = jQuery("#select_id :selected").text();          // 3.獲取Select選中項的Text    
  9. var checkIndex = jQuery("#select_id").attr("selectedIndex");    // 4.獲取Select選中項的索引值,或者:jQuery("#select_id").get(0).selectedIndex;   
  10. var maxIndex = jQuery("#select_id :last").attr("index");        // 5.獲取Select最大的索引值,或者:jQuery("#select_id :last").get(0).index;   
  11. /**
  12. * jQuery設置Select的選中項
  13. */   
  14. jQuery("#select_id").get(0).selectedIndex = 1;                  // 1.設置Select索引值爲1的項選中   
  15. jQuery("#select_id").val(4);                                    // 2.設置Select的Value值爲4的項選中   
  16. /**
  17. * jQuery添加/刪除Select的Option項
  18. */   
  19. jQuery("#select_id").append("<option value='新增'>新增option</option>");    // 1.爲Select追加一個Option(下拉項)    
  20. jQuery("#select_id").prepend("<option value='請選擇'>請選擇</option>");   // 2.爲Select插入一個Option(第一個位置)   
  21. jQuery("#select_id").get(0).remove(1);                                      // 3.刪除Select中索引值爲1的Option(第二個)   
  22. jQuery("#select_id :last").remove();                                        // 4.刪除Select中索引值最大Option(最後一個)    
  23. jQuery("#select_id [value='3']").remove();                                  // 5.刪除Select中Value='3'的Option    
  24. jQuery("#select_id").empty();                                               // 6.清空下拉列表   
相關文章
相關標籤/搜索