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)文章
jQuery實(shí)現(xiàn)點(diǎn)擊后標(biāo)記當(dāng)前菜單位置(背景高亮菜單)效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)點(diǎn)擊后標(biāo)記當(dāng)前菜單位置(背景高亮菜單)效果,涉及jquery鼠標(biāo)點(diǎn)擊事件及鏈?zhǔn)讲僮骺刂圃貥邮絼?dòng)態(tài)變換的技巧,需要的朋友可以參考下2015-08-08用示例說(shuō)明filter()與find()的用法以及children()與find()的區(qū)別分析
本篇文章介紹了,用示例說(shuō)明filter()與find()的用法以及children()與find()的區(qū)別分析。需要的朋友參考下2013-04-04jquery動(dòng)態(tài)改變onclick屬性導(dǎo)致失效的問(wèn)題解決方法
onclick屬性失效的問(wèn)題,相信很多的朋友都有遇到過(guò)吧,jquery動(dòng)態(tài)改變onclick屬性就會(huì)導(dǎo)致此問(wèn)題的發(fā)生,解決方法如下,希望對(duì)大家有所幫助2013-12-12jQuery實(shí)現(xiàn)動(dòng)態(tài)添加和刪除一個(gè)div
我想做一個(gè)可以動(dòng)態(tài)添加刪除div的功能。中間遇到一個(gè)問(wèn)題,最后在網(wǎng)友的熱心幫助下解決了,使用到的jquery方法和思想就是:事件的綁定和銷毀(unbind)2015-08-08jquery animate 動(dòng)畫效果使用說(shuō)明
jquery animate 動(dòng)畫效果使用說(shuō)明,需要的朋友可以參考下。2009-11-11jQuery 獲取select選中值及清除選中狀態(tài)
這篇文章主要介紹了jQuery 獲取select選中值及清除選中狀態(tài)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12jQuery noConflict() 方法用法實(shí)例分析
這篇文章主要介紹了jQuery noConflict() 方法用法,結(jié)合實(shí)例形式分析了jQuery noConflict() 方法基本功能、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-05-05