$("input[id^='code']");//id屬性以code開始的全部input標籤 $("input[id$='code']");//id屬性以code結束的全部input標籤 $("input[id*='code']");//id屬性包含code的全部input標籤 $("input[name^='code']");//name屬性以code開始的全部input標籤 $("input[name$='code']");//name屬性以code結束的全部input標籤 $("input[name*='code']");//name屬性包含code的全部input標籤
如今咱們如下面的html爲例進行checkbox的操做。javascript
<input id="checkAll" type="checkbox" />全選 <input name="subBox" type="checkbox" />項1 <input name="subBox" type="checkbox" />項2 <input name="subBox" type="checkbox" />項3 <input name="subBox" type="checkbox" />項4
<script type="text/javascript"> $(function() { $("#checkAll").click(function() { $('input[name="subBox"]').attr("checked",this.checked); }); var $subBox = $("input[name='subBox']"); $subBox.click(function(){ $("#checkAll").attr("checked",$subBox.length == $("input[name='subBox']:checked").length ? true : false); }); }); </script>
var val = $("#checkAll").val();// 獲取指定id的複選框的值
$('#checkAll').prop('checked');//jquery較新版本使用prop
var isSelected = $("#checkAll").attr("checked"); // 判斷id=checkAll的那個複選框是否處於選中狀態,選中則isSelected=true;不然isSelected=false; $("#checkAll").attr("checked", true);// or $("#checkAll").attr("checked", 'checked');// 將id=checkbox_id3的那個複選框選中,即打勾 $("#checkAll").attr("checked", false);// or $("#checkAll").attr("checked", '');// 將id=checkbox_id3的那個複選框不選中,即不打勾 $("input[name=subBox][value=3]").attr("checked", 'checked');// 將name=subBox, value=3 的那個複選框選中,即打勾 $("input[name=subBox][value=3]").attr("checked", '');// 將name=subBox, value=3 的那個複選框不選中,即不打勾 $("input[type=checkbox][name=subBox]").get(2).checked = true;// 設置index = 2,即第三項爲選中狀態 $("input[type=checkbox]:checked").each(function(){ //因爲複選框通常選中的是多個,因此能夠循環輸出選中的值 alert($(this).val()); });
咱們仍然如下面的html爲例:html
<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
radio操做以下:java
$("input[name=radio]:eq(0)").attr("checked",'checked'); //這樣就是第一個選中咯。 //jquery中,radio的選中與否和checkbox是同樣的。 $("#radio1").attr("checked","checked"); $("#radio1").removeAttr("checked"); $("input[type='radio'][name='radio']:checked").length == 0 ? "沒有任何單選框被選中" : "已經有選中"; $('input[type="radio"][name="radio"]:checked').val(); // 獲取一組radio被選中項的值 $("input[type='radio'][name='radio'][value='2']").attr("checked", "checked");// 設置value = 2的一項爲選中 $("#radio2").attr("checked", "checked"); // 設置id=radio2的一項爲選中 $("input[type='radio'][name='radio']").get(1).checked = true; // 設置index = 1,即第二項爲當前選中 var isChecked = $("#radio2").attr("checked");// id=radio2的一項處於選中狀態則isChecked = true, 不然isChecked = false; var isChecked = $("input[type='radio'][name='radio'][value='2']").attr("checked");// value=2的一項處於選中狀態則isChecked = true, 不然isChecked = false;
select操做相比checkbox和radio要相對麻煩一些,咱們仍然如下面的html爲例來講明:jquery
<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>
看select的以下屬性:app
$("#select_id").change(function(){ // 1.爲Select添加事件,當選擇其中一項時觸發 //code... }); var checkValue = $("#select_id").val(); // 2.獲取Select選中項的Value var checkText = $("#select_id :selected").text(); // 3.獲取Select選中項的Text var checkIndex = $("#select_id").attr("selectedIndex"); // 4.獲取Select選中項的索引值,或者:$("#select_id").get(0).selectedIndex; var maxIndex =$("#select_id :last").get(0).index; // 5.獲取Select最大的索引值 /** * jQuery設置Select的選中項 */ $("#select_id").get(0).selectedIndex = 1; // 1.設置Select索引值爲1的項選中 $("#select_id").val(4); // 2.設置Select的Value值爲4的項選中 /** * jQuery添加/刪除Select的Option項 */ $("#select_id").append("<option value='新增'>新增option</option>"); // 1.爲Select追加一個Option(下拉項) $("#select_id").prepend("<option value='請選擇'>請選擇</option>"); // 2.爲Select插入一個Option(第一個位置) $("#select_id").get(0).remove(1); // 3.刪除Select中索引值爲1的Option(第二個) $("#select_id :last").remove(); // 4.刪除Select中索引值最大Option(最後一個) $("#select_id [value='3']").remove(); // 5.刪除Select中Value='3'的Option $("#select_id").empty(); $("#select_id").find("option:selected").text(); // 獲取select 選中的 text : $("#select_id").val(); // 獲取select選中的 value: $("#select_id").get(0).selectedIndex; // 獲取select選中的索引: //設置select 選中的value: $("#select_id").attr("value","Normal"); $("#select_id").val("Normal"); $("#select_id").get(0).value = value; //設置select 選中的text,一般能夠在select回填中使用 var numId=33 //設置text==33的選中! var count=$("#select_id option").length; for(var i=0;i<count;i++) { if($("#select_id").get(0).options[i].text == numId) { $("#select_id").get(0).options[i].selected = true; break; } }