jQuery獲取Select選擇的Text和Value:
$("#select_id").change(function(){//code...}); //為Select添加事件,當(dāng)選擇其中一項(xiàng)時(shí)觸發(fā)varcheckText=$("#select_id").find("option:selected").text();//獲取Select選擇的textvarcheckValue=$("#select_id").val();//獲取Select選擇的ValuevarcheckIndex=$("#select_id ").get(0).selectedIndex;//獲取Select選擇的索引值varmaxIndex=$("#select_id option:last").attr("index");//獲取Select最大的索引值
jQuery添加/刪除Select的Option項(xiàng):
$("#select_id").append("
");//為Select追加一個(gè)Option(下拉項(xiàng))$("#select_id").prepend("
");//為Select插入一個(gè)Option(第一個(gè)位置)$("#select_id option:last").remove();//刪除Select中索引值最大Option(最后一個(gè))$("#select_id option[index='0']").remove();//刪除Select中索引值為0的Option(第一個(gè))$("#select_id option[value='3']").remove();//刪除Select中Value='3'的Optiona$("#select_id option[text='4']").remove();//刪除Select中Text='4'的Optiona
內(nèi)容清空:
$("#charCity").empty();
每一次操作select的時(shí)候,總是要出來(lái)翻一下資料,不如自己總結(jié)一下,以后就翻這里了。
比如
1、設(shè)置value為pxx的項(xiàng)選中
$(".selector").val("pxx");
2、設(shè)置text為pxx的項(xiàng)選中
$(".selector").find("option[text='pxx']").attr("selected",true);
這里有一個(gè)中括號(hào)的用法,中括號(hào)里的等號(hào)的前面是屬性名稱(chēng),不用加引號(hào)。很多時(shí)候,中括號(hào)的運(yùn)用可以使得邏輯變得很簡(jiǎn)單。
3、獲取當(dāng)前選中項(xiàng)的value
$(".selector").val();
4、獲取當(dāng)前選中項(xiàng)的text
$(".selector").find("option:selected").text();
這里用到了冒號(hào),掌握它的用法并舉一反三也會(huì)讓代碼變得簡(jiǎn)潔。
很多時(shí)候用到select的級(jí)聯(lián),即第二個(gè)select的值隨著第一個(gè)select選中的值變化。這在jquery中是非常簡(jiǎn)單的。
如:
$(".selector1").change(function(){//先清空第二個(gè)$(".selector2").empty();//實(shí)際的應(yīng)用中,這里的option一般都是用循環(huán)生成多個(gè)了varoption = $("
$(".selector2").append(option);
});
jQuery獲取Select選擇的Text和Value:
語(yǔ)法解釋?zhuān)?/p>
$("#select_id").change(function(){//code...});? //為Select添加事件,當(dāng)選擇其中一項(xiàng)時(shí)觸發(fā)
var checkText=$("#select_id").find("option:selected").text();? //獲取Select選擇的Text
var checkValue=$("#select_id").val();? //獲取Select選擇的Value
var checkIndex=$("#select_id ").get(0).selectedIndex;? //獲取Select選擇的索引值
var maxIndex=$("#select_id option:last").attr("index");? //獲取Select最大的索引值
jQuery設(shè)置Select選擇的 Text和Value:
語(yǔ)法解釋?zhuān)?/p>
$("#select_id ").get(0).selectedIndex=1;? //設(shè)置Select索引值為1的項(xiàng)選中
$("#select_id ").val(4);? // 設(shè)置Select的Value值為4的項(xiàng)選中
$("#select_id option[text='jQuery']").attr("selected", true);? //設(shè)置Select的Text值為jQuery的項(xiàng)選中
jQuery添加/刪除Select的Option項(xiàng):
語(yǔ)法解釋?zhuān)?/p>
$("#select_id").append("Text");//為Select追加一個(gè)Option(下拉項(xiàng))$("#select_id").prepend("請(qǐng)選擇");//為Select插入一個(gè)Option(第一個(gè)位置)$("#select_id option:last").remove();//刪除Select中索引值最大Option(最后一個(gè))$("#select_id option[index='0']").remove();//刪除Select中索引值為0的Option(第一個(gè))$("#select_id option[value='3']").remove();//刪除Select中Value='3'的Option$("#select_id option[text='4']").remove();//刪除Select中Text='4'的Option
jquery radio取值,checkbox取值,select取值,radio選中,checkbox選中,select選中,及其相關(guān)
獲 取一組radio被選中項(xiàng)的值
var item = $('input[name=items][checked]').val();
獲 取select被選中項(xiàng)的文本
var item = $("select[name=items] option[selected]").text();
select下拉框的第二個(gè)元素為當(dāng)前選中值
$('#select_id')[0].selectedIndex = 1;
radio單選組的第二個(gè)元素為當(dāng)前選中值
$('input[name=items]').get(1).checked = true;
獲取值:
文本框,文本區(qū)域:$("#txt").attr("value");
多選框 checkbox:$("#checkbox_id").attr("value");
單選組radio:?? $("input[type=radio][checked]").val();
下拉框select: $('#sel').val();
控制表單元素:
文本框,文本區(qū)域:$("#txt").attr("value",'');//清空內(nèi)容
$("#txt").attr("value",'11');//填充內(nèi)容
多選框checkbox: $("#chk1").attr("checked",'');//不打勾
$("#chk2").attr("checked",true);//打勾
if($("#chk1").attr('checked')==undefined) //判斷是否已經(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)
$("11112222").appendTo("#sel")//添加下拉框的option
$("#sel").empty();//清空下拉框