jquery 獲取表單元素里面的值示例代碼
更新時(shí)間:2013年07月28日 15:09:38 作者:
本文為大家詳細(xì)介紹下通過jquery獲取表單元素CheckBox、Radio等的值,有需求的朋友可以參考下,希望對大家有所幫助
jquery 筆記:
$(“input[name='radio_name']:checked”).val()
<input type="radio" value="1" name="radio_name" />1
<input type="radio" value="2" name="radio_name" />2
<input type="radio" value="3" name="radio_name" />3
網(wǎng)上的東西太亂了,而且jQuery不同版本可能寫法不太一樣,經(jīng)過搜索和做實(shí)驗(yàn),下面寫的是jQuery 1.3.2版本下的
jquery radio取值,checkbox取值,select取值,radio選中,checkbox選中,select選中,及其相關(guān)設(shè)置:
獲取一組radio被選中項(xiàng)的值:var item = $('input[name=items][checked]‘).val();
獲取select被選中項(xiàng)的文本
var item = $(”select[@name=items] option[@selected]“).text();
獲取select被選中項(xiàng)的文本 :var item = $(”select[name=items] option[selected]“).text(); 或
$(”select[name=items]“).find(”option:selected”).text();
select下拉框的第二個元素為當(dāng)前選中值:$('#select_id')[0].selectedIndex = 1;
select下拉框value = ‘val'的元素為當(dāng)前選中項(xiàng):$(”select[name=items] option[value='val']“).attr(”selected”,”selected”);
radio單選組的第二個元素為當(dāng)前選中項(xiàng) :$('input[@name=items]‘).get(1).checked = true; 或$('input[name=items]‘).attr(”checked”,1′);
radio的value = ‘val'的元素為當(dāng)前選中項(xiàng):$('input[name=items][value='val']‘).attr(”checked”,”checked”);
獲取值:
文本框,文本區(qū)域:$(”#txt”).attr(”value”);
多選框checkbox:$(”input[name='checkbox':checked]“).each(function(){
var val = $(this).val();
});
單選組radio: $(”input[type=radio][checked]“).val();
下拉框select的value值: $('select').val();
下拉框select選中的text值:$(”select”).find(”option:selected”).text();
控制表單元素:
文本框,文本區(qū)域:$(”#txt”).attr(”value”,”); //清空內(nèi)容
$(”#txt”).attr(”value”,'11′); //填充內(nèi)容
多選框checkbox:
checkbox的第二個元素被打勾:$(”input[name=items]“).get(1).checked = true; //打勾
$(”input[name=items]“).get(1).checked = false; //不打勾
checkbox的value='val'的元素前打勾:$(”input[name=item][value='val']“).attr(”checked”,true);或$(”input[name=item][value='val']“).attr(”checked”,”checked”);
if($(”input[name=item][value='val']“).attr('checked')==true) //判斷是否已經(jīng)打勾
單選組radio: $(”input[type=radio]“).attr(”checked”,'2′);//設(shè)置value=2的項(xiàng)目為當(dāng)前選中項(xiàng)
下拉框select: $(”#sel”).attr(”value”,'-sel3′);//設(shè)置value=-sel3的項(xiàng)目為當(dāng)前選中項(xiàng)
$(”<option value='1′>1111</option><option value='2′>2222</option>”).appendTo(”#sel”)//添加下拉框的option
$(”#sel”).empty();//清空下拉框
jQuery獲取Radio選擇的Value值
代碼$("input[name='radio_name'][checked]").val(); //選擇被選中Radio的Value值
$("#text_id").focus(function(){//code...}); //事件 當(dāng)對象text_id獲取焦點(diǎn)時(shí)觸發(fā)
$("#text_id").blur(function(){//code...}); //事件 當(dāng)對象text_id失去焦點(diǎn)時(shí)觸發(fā)
$("#text_id").select(); //使文本框的Vlaue值成選中狀態(tài)
$("input[name='radio_name'][value='要選中Radio的Value值'").
attr("checked",true); //根據(jù)Value值設(shè)置Radio為選中狀態(tài)
jQuery獲取CheckBox選擇的Value值
$("input[name='checkbox_name'][checked]"); //選擇被選中CheckBox元素的集合 如果你想得到Value值你需要遍歷這個集合
$($("input[name='checkbox_name'][checked]")).
each(function(){arrChk+=this.value +',';});//遍歷被選中CheckBox元素的集合 得到Value值
$("#checkbox_id").attr("checked"); //獲取一個CheckBox的狀態(tài)(有沒有被選中,返回true/false)
$("#checkbox_id").attr("checked",true); //設(shè)置一個CheckBox的狀態(tài)為選中(checked=true)
$("#checkbox_id").attr("checked",false); //設(shè)置一個CheckBox的狀態(tài)為不選中(checked=false)
$("input[name='checkbox_name']").attr
("checked",$("#checkbox_id").attr("checked"));//根據(jù)3,4,5條,你可以分析分析這句代碼的意思
$("#text_id").val().split(","); //將Text的Value值以','分隔 返回一個數(shù)組
上面的這些操作,其實(shí)就是jQuery選擇器的使用,希望大家對jQuery選擇器方面的知識要掌握扎實(shí)。
復(fù)制代碼 代碼如下:
$(“input[name='radio_name']:checked”).val()
<input type="radio" value="1" name="radio_name" />1
<input type="radio" value="2" name="radio_name" />2
<input type="radio" value="3" name="radio_name" />3
網(wǎng)上的東西太亂了,而且jQuery不同版本可能寫法不太一樣,經(jīng)過搜索和做實(shí)驗(yàn),下面寫的是jQuery 1.3.2版本下的
jquery radio取值,checkbox取值,select取值,radio選中,checkbox選中,select選中,及其相關(guān)設(shè)置:
獲取一組radio被選中項(xiàng)的值:var item = $('input[name=items][checked]‘).val();
獲取select被選中項(xiàng)的文本
var item = $(”select[@name=items] option[@selected]“).text();
獲取select被選中項(xiàng)的文本 :var item = $(”select[name=items] option[selected]“).text(); 或
$(”select[name=items]“).find(”option:selected”).text();
select下拉框的第二個元素為當(dāng)前選中值:$('#select_id')[0].selectedIndex = 1;
select下拉框value = ‘val'的元素為當(dāng)前選中項(xiàng):$(”select[name=items] option[value='val']“).attr(”selected”,”selected”);
radio單選組的第二個元素為當(dāng)前選中項(xiàng) :$('input[@name=items]‘).get(1).checked = true; 或$('input[name=items]‘).attr(”checked”,1′);
radio的value = ‘val'的元素為當(dāng)前選中項(xiàng):$('input[name=items][value='val']‘).attr(”checked”,”checked”);
獲取值:
文本框,文本區(qū)域:$(”#txt”).attr(”value”);
多選框checkbox:$(”input[name='checkbox':checked]“).each(function(){
var val = $(this).val();
});
單選組radio: $(”input[type=radio][checked]“).val();
下拉框select的value值: $('select').val();
下拉框select選中的text值:$(”select”).find(”option:selected”).text();
控制表單元素:
文本框,文本區(qū)域:$(”#txt”).attr(”value”,”); //清空內(nèi)容
$(”#txt”).attr(”value”,'11′); //填充內(nèi)容
多選框checkbox:
checkbox的第二個元素被打勾:$(”input[name=items]“).get(1).checked = true; //打勾
$(”input[name=items]“).get(1).checked = false; //不打勾
checkbox的value='val'的元素前打勾:$(”input[name=item][value='val']“).attr(”checked”,true);或$(”input[name=item][value='val']“).attr(”checked”,”checked”);
if($(”input[name=item][value='val']“).attr('checked')==true) //判斷是否已經(jīng)打勾
單選組radio: $(”input[type=radio]“).attr(”checked”,'2′);//設(shè)置value=2的項(xiàng)目為當(dāng)前選中項(xiàng)
下拉框select: $(”#sel”).attr(”value”,'-sel3′);//設(shè)置value=-sel3的項(xiàng)目為當(dāng)前選中項(xiàng)
$(”<option value='1′>1111</option><option value='2′>2222</option>”).appendTo(”#sel”)//添加下拉框的option
$(”#sel”).empty();//清空下拉框
jQuery獲取Radio選擇的Value值
代碼$("input[name='radio_name'][checked]").val(); //選擇被選中Radio的Value值
$("#text_id").focus(function(){//code...}); //事件 當(dāng)對象text_id獲取焦點(diǎn)時(shí)觸發(fā)
$("#text_id").blur(function(){//code...}); //事件 當(dāng)對象text_id失去焦點(diǎn)時(shí)觸發(fā)
$("#text_id").select(); //使文本框的Vlaue值成選中狀態(tài)
$("input[name='radio_name'][value='要選中Radio的Value值'").
attr("checked",true); //根據(jù)Value值設(shè)置Radio為選中狀態(tài)
jQuery獲取CheckBox選擇的Value值
$("input[name='checkbox_name'][checked]"); //選擇被選中CheckBox元素的集合 如果你想得到Value值你需要遍歷這個集合
$($("input[name='checkbox_name'][checked]")).
each(function(){arrChk+=this.value +',';});//遍歷被選中CheckBox元素的集合 得到Value值
$("#checkbox_id").attr("checked"); //獲取一個CheckBox的狀態(tài)(有沒有被選中,返回true/false)
$("#checkbox_id").attr("checked",true); //設(shè)置一個CheckBox的狀態(tài)為選中(checked=true)
$("#checkbox_id").attr("checked",false); //設(shè)置一個CheckBox的狀態(tài)為不選中(checked=false)
$("input[name='checkbox_name']").attr
("checked",$("#checkbox_id").attr("checked"));//根據(jù)3,4,5條,你可以分析分析這句代碼的意思
$("#text_id").val().split(","); //將Text的Value值以','分隔 返回一個數(shù)組
上面的這些操作,其實(shí)就是jQuery選擇器的使用,希望大家對jQuery選擇器方面的知識要掌握扎實(shí)。
相關(guān)文章
Jquery讓form表單異步提交代碼實(shí)現(xiàn)
這篇文章主要介紹了Jquery讓form表單異步提交代碼實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
jquery使用slideDown實(shí)現(xiàn)模塊緩慢拉出效果的方法
這篇文章主要介紹了jquery使用slideDown實(shí)現(xiàn)模塊緩慢拉出效果的方法,涉及slideDown方法操作模塊展示效果的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
jQuery清除定時(shí)任務(wù)的實(shí)現(xiàn)
jQuery中使用setInterval函數(shù)設(shè)置定時(shí)任務(wù),并通過clearInterval函數(shù)來清除這些任務(wù),從而避免資源浪費(fèi)或邏輯混亂,具有一定的參考價(jià)值,感興趣的可以了解一下2024-09-09
基于jQuery實(shí)現(xiàn)仿微博發(fā)布框字?jǐn)?shù)提示
這篇文章主要為大家詳細(xì)介紹了基于jQuery實(shí)現(xiàn)仿微博發(fā)布框字?jǐn)?shù)提示的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
jQuery 開發(fā)之EasyUI 添加數(shù)據(jù)的實(shí)例
這篇文章主要介紹了jQuery 開發(fā)之EasyUI 添加數(shù)據(jù)的實(shí)例的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這部分內(nèi)容,需要的朋友可以參考下2017-09-09
jquery+css3實(shí)現(xiàn)的經(jīng)典彈出層效果示例
這篇文章主要介紹了jquery+css3實(shí)現(xiàn)的經(jīng)典彈出層效果,結(jié)合實(shí)例形式分析了jquery+css3實(shí)現(xiàn)彈出層具體原理、步驟與相關(guān)操作技巧,需要的朋友可以參考下2020-05-05
jQuery on()方法綁定動態(tài)元素的點(diǎn)擊事件無響應(yīng)的解決辦法
這篇文章主要介紹了jQuery on()方法綁定動態(tài)元素的點(diǎn)擊事件無響應(yīng)的解決辦法的相關(guān)資料,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07

