jQuery獲取多種input值的簡(jiǎn)單實(shí)現(xiàn)方法
獲取input的checked值是否為true:
第一種:
if($("input[name=item][value='val']").attr('checked')==true) //判斷是否已經(jīng)打勾 --注:name即控件name屬性,value即控件value屬性
第二種:
可以不指定屬性值,因一組checkbox的value值都會(huì)保存其在數(shù)據(jù)庫(kù)中對(duì)應(yīng)的id,最好寫成如下方式:
if($("input[name=row_checkbox]").attr('checked')==true)
第三種:
if($("[name=row_checkbox]").attr('checked')==true) --注:name即控件name屬性
jquery radio取值,checkbox取值,select取值,radio選中,checkbox選中,select選中,及其相關(guān)文章分類:Web前端:
radio:
獲取一組radio被選中項(xiàng)的值:var item = $('input[name=items][checked]').val(); --注:name即控件name屬性
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");
radio設(shè)置value=2的元素為當(dāng)前選中項(xiàng):$("input[type=radio]").attr("checked",'2′);
radio被選中項(xiàng)的value值:$("input[name='radio_name'][checked]").val();
根據(jù)Value值設(shè)置Radio為選中狀態(tài):$("input[name='radio_name'][value='要選中Radio的Value值'").attr("checked",true);
select:
獲取select被選中項(xiàng)的文本:var item = $("select[@name=items] option[@selected]").text();
或 var item = $("select[name=items]").find("option:selected").text();
select下拉框的第二個(gè)元素為當(dāng)前選中值:$('#select_id')[0].selectedIndex = 1; --注:select_id'即控件的id屬性
select下拉框value = 'val'的元素為當(dāng)前選中項(xiàng):$("select[name=items] option[value='val']").attr("selected","selected");
select設(shè)置value=-sel3的項(xiàng)目為當(dāng)前選中項(xiàng):$("#sel").attr("value",'-sel3′); --注:sel即select控件的id屬性
添加下拉框的option:$("<option value='1′>1111</option><option value='2′>2222</option>").appendTo("#sel");
select清空:$("#sel").empty();
checkbox:
checkbox的第二個(gè)元素被打勾:$("input[name=items]").get(1).checked = true; //打勾
checkbox的value='val'的元素打勾:$("input[name=item][value='val']").attr("checked",true);
或$("input[name=item][value='val']").attr("checked","checked");
判斷checkbox是否已經(jīng)打勾:if($("input[name=item][value='val']").attr('checked')==true)
jQuery獲取CheckBox選擇的Value值:
//選擇被選中CheckBox元素的集合 如果你想得到Value值你需要遍歷這個(gè)集合
$($("input[name='checkbox_name'][checked]")).each(function(){
arrChk+=this.value + ','; //遍歷被選中CheckBox元素的集合 得到Value值
});
checkbox的checked屬性:
$("#checkbox_id").attr("checked"); //獲取一個(gè)CheckBox的狀態(tài)(有沒(mé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)
//根據(jù)上面三條,分析分析這句代碼的意思:
$("input[name='checkbox_name']").attr("checked",$("#checkbox_id").attr("checked"));
--注:根據(jù)控件checkbox_id的checked狀態(tài)為name='checkbox_name'的input賦相同的checked狀態(tài)
獲取值:
文本框,文本區(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)容
事件:
當(dāng)對(duì)象text_id獲取焦點(diǎn)時(shí)觸發(fā):$("#text_id").focus(function(){//code...});
當(dāng)對(duì)象text_id失去焦點(diǎn)時(shí)觸發(fā):$("#text_id").blur(function(){//code...});
其他:
使文本框的Vlaue值成選中狀態(tài):$("#text_id").select();
$("#text_id").val().split(","); //將Text的Value值以','分隔返回一個(gè)數(shù)組
以上這篇jQuery獲取多種input值的簡(jiǎn)單實(shí)現(xiàn)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
使用DataTable插件實(shí)現(xiàn)異步加載數(shù)據(jù)
本文給大家分享的是Jquery+dataTable插件來(lái)實(shí)現(xiàn)異步加載數(shù)據(jù)的示例代碼,非常實(shí)用,有需要的小伙伴可以參考下2017-11-11
jquery實(shí)現(xiàn)上傳文件大小類型的驗(yàn)證例子(推薦)
下面小編就為大家?guī)?lái)一篇jquery實(shí)現(xiàn)上傳文件大小類型的驗(yàn)證例子(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨想過(guò)來(lái)看看吧2016-06-06
jQuery的cookie插件實(shí)現(xiàn)保存用戶登陸信息
保存用戶登陸信息的方法有很多,本文為大家介紹的這個(gè)方法是通過(guò)cookie插件來(lái)實(shí)現(xiàn),需要的朋友可以參考下2014-04-04
【經(jīng)典源碼收藏】jQuery實(shí)用代碼片段(篩選,搜索,樣式,清除默認(rèn)值,多選等)
這篇文章主要介紹了jQuery實(shí)用代碼片段,包括篩選,搜索,樣式,清除默認(rèn)值,多選等多種功能.具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
用jQuery與JSONP輕松解決跨域訪問(wèn)的問(wèn)題
本篇文章主要是對(duì)使用jQuery與JSONP輕松解決跨域訪問(wèn)的問(wèn)題進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02
Jquery綁定事件(bind和live的區(qū)別介紹)
Jquery中綁定事件有三種方法click、bind、live第一種方法很好理解,其實(shí)就和普通JS的用法差不多,只是少了一個(gè)on而已第二、三種方法都是綁定事件2013-08-08
jQuery學(xué)習(xí)筆記之DOM對(duì)象和jQuery對(duì)象
DOM對(duì)象和jQuery對(duì)象的操作時(shí)很重要的,是jquery與頁(yè)面元素互動(dòng)的基礎(chǔ)。2010-12-12

