Jquery操作radio,checkbox,select表單操作實(shí)現(xiàn)代碼
一 、Select
jQuery獲取Select選擇的Text和Value:
1. $("#select_id").change(function(){//code...}); //為Select添加事件,當(dāng)選擇其中一項(xiàng)時觸發(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的項(xiàng)選中
2. $("#select_id ").val(4); //設(shè)置Select的Value值為4的項(xiàng)選中
3. $("#select_id option[text='jQuery']").attr("selected", true); //設(shè)置Select的Text值為jQuery的項(xiàng)選中
jQuery添加/刪除Select的Option項(xiàng):
1. $("#select_id").append("<option value='Value'>Text</option>"); //為Select追加一個Option(下拉項(xiàng))
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(); //刪除所有項(xiàng)
二、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值
下面的代碼是獲取上面實(shí)例中選中的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è)置選中項(xiàng)
$("input[type='checkbox']").eq(1).attr("checked")//設(shè)置第一個checkbox為選中的項(xiàng)
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è)置指定的項(xiàng)為當(dāng)前選中項(xiàng)
$("input[type='radio']").eq(1).attr("checked", true);//設(shè)置第二項(xiàng)為選中項(xiàng)
$("input[type='radio'][value='值']").attr("checked, true");
3,解決多個Radio
$("input[type='radio'][@name='rdoTest2']").eq(0).attr("checked", true);
學(xué)習(xí)筆記,以備后用。
運(yùn)行以后,請刷新下才能看到效果,保存到本地運(yùn)行,沒有任何問題。
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁面才能執(zhí)行]
相關(guān)文章
使用jQuery判斷Div是否在可視區(qū)域的方法 判斷div是否可見
這篇文章主要介紹了使用jQuery判斷Div是否在可視區(qū)域的方法 判斷div是否可見2016-02-02js jquery獲取當(dāng)前元素的兄弟級 上一個 下一個元素
js獲取方法要比jq的方法麻煩的多,主要是因?yàn)镕F瀏覽器,因?yàn)镕F瀏覽器也會把換行當(dāng)作dom元素,下面跟著小編一起來學(xué)習(xí)js jquery獲取當(dāng)前元素的兄弟級 上一個 下一個元素的,需要的朋友一起來學(xué)習(xí)吧2015-09-09jquery實(shí)現(xiàn)TAB選項(xiàng)卡鼠標(biāo)經(jīng)過帶延遲效果的方法
這篇文章主要介紹了jquery實(shí)現(xiàn)TAB選項(xiàng)卡鼠標(biāo)經(jīng)過帶延遲效果的方法,可實(shí)現(xiàn)tab選項(xiàng)卡的延遲加載效果,涉及jquery鼠標(biāo)事件及延遲函數(shù)的相關(guān)使用技巧,需要的朋友可以參考下2015-07-07jquery實(shí)現(xiàn)側(cè)邊彈出的垂直導(dǎo)航
這篇文章主要介紹了jquery動畫特效結(jié)合css實(shí)現(xiàn)側(cè)邊彈出的垂直導(dǎo)航的方法及代碼分享,需要的朋友可以參考下2014-12-12jquery京東商城雙11焦點(diǎn)圖多圖廣告特效代碼分享
這篇文章主要介紹了jquery京東商城雙11焦點(diǎn)圖多圖廣告特效,一個精致的焦點(diǎn)圖會吸引用戶的注意力,讓用戶產(chǎn)生瀏覽網(wǎng)站的興趣至關(guān)重要,現(xiàn)在小編推薦給大家一款特別棒的焦點(diǎn)圖,感興趣的小伙伴可以參考下。2015-09-09jquery ajax雙擊div可直接修改div中的內(nèi)容
這篇文章主要介紹了jquery ajax雙擊div直接修改div中內(nèi)容的相關(guān)方法,感興趣的小伙伴們可以參考一下2016-03-03