jquery radio取值,checkbox取值,select取值,radio選中,checkbox選中,select選中,及其相關

獲取一組radio被選中項的值 
var item = $('input[@name=items][@checked]').val(); 
獲取select被選中項的文本 
var item = $("select[@name=items] option[@selected]").text(); 
select下拉框的第二個元素爲當前選中值 
$('#select_id')[0].selectedIndex = 1; 
radio單選組的第二個元素爲當前選中值 
$('input[@name=items]').get(1).checked = true; 

獲取值: 

文本框,文本區域:$("#txt").attr("value"); 
多選框checkbox:$("#checkbox_id").attr("value"); 
單選組radio:   $("input[@type=radio][@checked]").val(); 
下拉框select: $('#sel').val(); 

控制表單元素: 
文本框,文本區域:$("#txt").attr("value",'');//清空內容 
                 $("#txt").attr("value",'11');//填充內容 

多選框checkbox: $("#chk1").attr("checked",'');//不打勾 
                 $("#chk2").attr("checked",true);//打勾 
                 if($("#chk1").attr('checked')==undefined) //判斷是否已經打勾 

單選組radio:    $("input[@type=radio]").attr("checked",'2');//設置value=2的項目爲當前選中項 
下拉框select:   $("#sel").attr("value",'-sel3');//設置value=-sel3的項目爲當前選中項 
                $("<option value='1'>1111</option><option                 value='2'>2222</option>").appendTo("#sel")//添加下拉框的option 
                $("#sel").empty();//清空下拉框 

一 、Select 
jQuery獲取Select選擇的Text和Value: 
1. $("#select_id").change(function(){//code...}); //爲Select添加事件,當選擇其中一項時觸發 
2. var checkText=$("#select_id").find("option:selected").text(); //獲取Select選擇的Text 
3. var checkValue=$("#select_id").val(); //獲取Select選擇的Value 
4. var checkIndex=$("#select_id ").get(0).selectedIndex; //獲取Select選擇的索引值 
5. var maxIndex=$("#select_id option:last").attr("index"); //獲取Select最大的索引值 

jQuery設置Select選擇的Text和Value: 
1. $("#select_id ").get(0).selectedIndex=1; //設置Select索引值爲1的項選中 
2. $("#select_id ").val(4); //設置Select的Value值爲4的項選中 
3. $("#select_id option[text='jQuery']").attr("selected", true); //設置Select的Text值爲jQuery的項選中 

jQuery添加/刪除Select的Option項: 
1. $("#select_id").append("<option value='Value'>Text</option>"); //爲Select追加一個Option(下拉項) 
2. $("#select_id").prepend("<option value='0'>請選擇</option>"); //爲Select插入一個Option(第一個位置) 
3. $("#select_id option:last").remove(); //刪除Select中索引值最大Option(最後一個) 
4. $("#select_id option[index='0']").remove(); //刪除Select中索引值爲0的Option(第一個) 
5. $("#select_id option[value='3']").remove(); //刪除Select中Value='3'的Option 
6. $("#select_id option[text='4']").remove(); //刪除Select中Text='4'的Option 
7. $("#SelectID").remove(); //刪除所有項 

二、Checkbox 
全選/取消 
jQuery.attr 獲取/設置對象的屬性值,如: 
$("input[name='chk_list']").attr("checked"); //讀取所有name爲'chk_list'對象的狀態(是否選中) 
$("input[name='chk_list']").attr("checked",true); //設置所有name爲'chk_list'對象的checked爲true 
$("#img_1").attr("src","test.jpg"); //設置ID爲img_1的<img>src的值爲'test.jpg' 
$("#img_1").attr("src"); //讀取ID爲img_1的<img>src值 
下面的代碼是獲取上面實例中選中的checkbox的value值: 
<script type="text/javascript"> 
var arrChk=$("input[name='chk_list'][checked]"); 
$(arrChk).each(function(){ 
window.alert(this.value); 
}); 
}); 
</script> 

1,獲取checkbox的value 
$("#checkboxID").value或$("input[type='checkbox']").eq(n).attr("checked").value 
2,設置選中項 
$("input[type='checkbox']").eq(1).attr("checked")//設置第一個checkbox爲選中的項 
3,刪除所有checkbox 
$("input[type='checkbox']").remove() 
4,checkbox方法 
$(document).ready(function() { 
var check = $("input[type='checkbox']"); 
check.each(function(n) { 
check.eq(n).bind("click", function() { 
if (check.eq(n).attr("checked") != false) { 
var value = check.eq(n).val(); 
alert(value); 

else { 
alert(check.eq(n).attr("checked")); 

}) 
}); 
}); 

三、radio 
1,獲取選中的value值 
$("input[type='radio']:checked").val(); 
2,設置指定的項爲當前選中項 
$("input[type='radio']").eq(1).attr("checked", true);//設置第二項爲選中項 
$("input[type='radio'][value='值']").attr("checked, true"); 

3,解決多個Radio 

$("input[type='radio'][@name='rdoTest2']").eq(0).attr("checked", true); 
學習筆記,以備後用。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章