Jquery操作radio,checkbox,select表單操作實現(xiàn)代碼
一 、Select
jQuery獲取Select選擇的Text和Value:
1. $("#select_id").change(function(){//code...}); //為Select添加事件,當選擇其中一項時觸發(fā)
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設(shè)置Select選擇的Text和Value:
1. $("#select_id ").get(0).selectedIndex=1; //設(shè)置Select索引值為1的項選中
2. $("#select_id ").val(4); //設(shè)置Select的Value值為4的項選中
3. $("#select_id option[text='jQuery']").attr("selected", true); //設(shè)置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 獲取/設(shè)置對象的屬性值,如:
$("input[name='chk_list']").attr("checked"); //讀取所有name為'chk_list'對象的狀態(tài)(是否選中)
$("input[name='chk_list']").attr("checked",true); //設(shè)置所有name為'chk_list'對象的checked為true
$("#img_1").attr("src","test.jpg"); //設(shè)置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,設(shè)置選中項
$("input[type='checkbox']").eq(1).attr("checked")//設(shè)置第一個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,設(shè)置指定的項為當前選中項
$("input[type='radio']").eq(1).attr("checked", true);//設(shè)置第二項為選中項
$("input[type='radio'][value='值']").attr("checked, true");
3,解決多個Radio
$("input[type='radio'][@name='rdoTest2']").eq(0).attr("checked", true);
學習筆記,以備后用。
運行以后,請刷新下才能看到效果,保存到本地運行,沒有任何問題。
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
相關(guān)文章
使用jQuery判斷Div是否在可視區(qū)域的方法 判斷div是否可見
這篇文章主要介紹了使用jQuery判斷Div是否在可視區(qū)域的方法 判斷div是否可見2016-02-02jquery實現(xiàn)TAB選項卡鼠標經(jīng)過帶延遲效果的方法
這篇文章主要介紹了jquery實現(xiàn)TAB選項卡鼠標經(jīng)過帶延遲效果的方法,可實現(xiàn)tab選項卡的延遲加載效果,涉及jquery鼠標事件及延遲函數(shù)的相關(guān)使用技巧,需要的朋友可以參考下2015-07-07jquery ajax雙擊div可直接修改div中的內(nèi)容
這篇文章主要介紹了jquery ajax雙擊div直接修改div中內(nèi)容的相關(guān)方法,感興趣的小伙伴們可以參考一下2016-03-03