jquery 獲取表單元素里面的值示例代碼
更新時(shí)間:2013年07月28日 15:09:38 作者:
本文為大家詳細(xì)介紹下通過jquery獲取表單元素CheckBox、Radio等的值,有需求的朋友可以參考下,希望對(duì)大家有所幫助
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下拉框的第二個(gè)元素為當(dāng)前選中值:$('#select_id')[0].selectedIndex = 1;
select下拉框value = ‘val'的元素為當(dāng)前選中項(xiàng):$(”select[name=items] option[value='val']“).attr(”selected”,”selected”);
radio單選組的第二個(gè)元素為當(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的第二個(gè)元素被打勾:$(”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)對(duì)象text_id獲取焦點(diǎn)時(shí)觸發(fā)
$("#text_id").blur(function(){//code...}); //事件 當(dāng)對(duì)象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值你需要遍歷這個(gè)集合
$($("input[name='checkbox_name'][checked]")).
each(function(){arrChk+=this.value +',';});//遍歷被選中CheckBox元素的集合 得到Value值
$("#checkbox_id").attr("checked"); //獲取一個(gè)CheckBox的狀態(tài)(有沒有被選中,返回true/false)
$("#checkbox_id").attr("checked",true); //設(shè)置一個(gè)CheckBox的狀態(tài)為選中(checked=true)
$("#checkbox_id").attr("checked",false); //設(shè)置一個(gè)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值以','分隔 返回一個(gè)數(shù)組
上面的這些操作,其實(shí)就是jQuery選擇器的使用,希望大家對(duì)jQuery選擇器方面的知識(shí)要掌握扎實(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下拉框的第二個(gè)元素為當(dāng)前選中值:$('#select_id')[0].selectedIndex = 1;
select下拉框value = ‘val'的元素為當(dāng)前選中項(xiàng):$(”select[name=items] option[value='val']“).attr(”selected”,”selected”);
radio單選組的第二個(gè)元素為當(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的第二個(gè)元素被打勾:$(”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)對(duì)象text_id獲取焦點(diǎn)時(shí)觸發(fā)
$("#text_id").blur(function(){//code...}); //事件 當(dāng)對(duì)象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值你需要遍歷這個(gè)集合
$($("input[name='checkbox_name'][checked]")).
each(function(){arrChk+=this.value +',';});//遍歷被選中CheckBox元素的集合 得到Value值
$("#checkbox_id").attr("checked"); //獲取一個(gè)CheckBox的狀態(tài)(有沒有被選中,返回true/false)
$("#checkbox_id").attr("checked",true); //設(shè)置一個(gè)CheckBox的狀態(tài)為選中(checked=true)
$("#checkbox_id").attr("checked",false); //設(shè)置一個(gè)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值以','分隔 返回一個(gè)數(shù)組
上面的這些操作,其實(shí)就是jQuery選擇器的使用,希望大家對(duì)jQuery選擇器方面的知識(shí)要掌握扎實(shí)。
相關(guān)文章
JS遮罩層效果 兼容ie firefox jQuery遮罩層
史上最精簡(jiǎn),最強(qiáng)大的JS遮罩層效果,支持ie firefox jQuery遮罩層2010-07-07jQuery實(shí)現(xiàn)圣誕節(jié)禮物動(dòng)畫案例解析
這篇文章主要介紹了jQuery實(shí)現(xiàn)圣誕節(jié)禮物動(dòng)畫案例解析的相關(guān)資料,需要的朋友可以參考下2016-12-12jquery.gridrotator實(shí)現(xiàn)響應(yīng)式圖片展示畫廊效果
本教程將教大家制作一個(gè)jQuery響應(yīng)式圖片展示畫廊效果,所有圖片以網(wǎng)格的形式排列,然后定時(shí)隨機(jī)翻轉(zhuǎn)其中某些格子用來切換圖片。這種效果可以用來當(dāng)做背景或裝飾放在我們的網(wǎng)站上。2015-06-06JQuery select控件的相關(guān)操作實(shí)現(xiàn)代碼
JQuery獲取和設(shè)置Select選項(xiàng)方法匯總?cè)缦?,需要的朋友可以參考?/div> 2012-09-09jquery autocomplete自動(dòng)完成插件的的使用方法
最近剛開始學(xué)jquery,想實(shí)現(xiàn)類似GOOGLE搜索時(shí)自動(dòng)顯示出相關(guān)結(jié)果的效果。于是選擇了使用jquery autocomplete插件。2010-08-08最新評(píng)論