一、radio:單選框jquery
<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 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
<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 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:下拉框post
<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> /** * jQuery獲取select的各類值 */ jQuery("#select_id").change(function(){ // 1.爲Select添加事件,當選擇其中一項時觸發 //code... }); var checkValue = jQuery("#select_id").val(); // 2.獲取Select選中項的Value var checkText = jQuery("#select_id :selected").text(); // 3.獲取Select選中項的Text var checkIndex = jQuery("#select_id").attr("selectedIndex"); // 4.獲取Select選中項的索引值,或者:jQuery("#select_id").get(0).selectedIndex; var maxIndex = jQuery("#select_id :last").attr("index"); // 5.獲取Select最大的索引值,或者:jQuery("#select_id :last").get(0).index; /** * jQuery設置Select的選中項 */ jQuery("#select_id").get(0).selectedIndex = 1; // 1.設置Select索引值爲1的項選中 jQuery("#select_id").val(4); // 2.設置Select的Value值爲4的項選中 /** * jQuery添加/刪除Select的Option項 */ jQuery("#select_id").append("<option value='新增'>新增option</option>"); // 1.爲Select追加一個Option(下拉項) jQuery("#select_id").prepend("<option value='請選擇'>請選擇</option>"); // 2.爲Select插入一個Option(第一個位置) jQuery("#select_id").get(0).remove(1); // 3.刪除Select中索引值爲1的Option(第二個) jQuery("#select_id :last").remove(); // 4.刪除Select中索引值最大Option(最後一個) jQuery("#select_id [value='3']").remove(); // 5.刪除Select中Value='3'的Option jQuery("#select_id").empty(); // 6.清空下拉列表
轉載自:http://www.9958.pw/post/jquery_radio_checkbox_selectthis