jquery操作select 取值,設定選中)

2021-08-07 22:29:01 字數 3237 閱讀 8606

一、基礎取值問題

例如class="selector">select>

1、設定value為pxx的項選中

$(".selector").val("pxx");

2、設定text為pxx的項選中 $(".selector").find("option[text='pxx']").attr("selected",true);

$("#ownertype").find("option[value='01']").attr("selected",true);

3、獲取當前選中項的value

$(".selector").val();

4、獲取當前選中項的text

$(".selector").find("option:selected").text();

二、select的級聯

如:$(".selector1").change(function

());

三、jquery獲取select選擇的text和value

//為select新增事件,當選擇其中一項時觸發

1. $("#select_id").change(function

());

//獲取select選擇的text

2. $("#select_id").find("option:selected").text();

//獲取select選擇的value

3.var checkvalue=$("#select_id").val();

//獲取select選擇的索引值

4.var checkindex=$("#select_id ").get(0).selectedindex;

//獲取select最大的索引值

5.var maxindex=$("#select_id option:last").attr("index");

四、jquery設定select選擇的 text和value

//設定select索引值為1的項選中

1. $("#select_id ").get(0).selectedindex=1;

// 設定select的value值為4的項選中

2. $("#select_id ").val(4);

//設定select的text值為jquery的項選中

3. $("#select_id option[text='jquery']").attr("selected", true);

五、jquery新增/刪除select的option項

//為select追加乙個option(下拉項)

//為select插入乙個option(第乙個位置)

2. $("#select_id").prepend("請選擇");

//刪除select中索引值最大option(最後乙個)

3. $("#select_id option:last").remove();

//刪除select中索引值為0的option(第乙個)

4. $("#select_id option[index='0']").remove();

//刪除select中value='3'的option

5. $("#select_id option[value='3']").remove();

//刪除select中text='4'的option

6. $("#select_id option[text='4']").remove();

六、jquery radio取值,checkbox取值,select取值,radio選中,checkbox選中,select選中,及其相關

1.獲取一組radio被選中項的值 

var item = $('input[name=items][checked]').val();

2.獲取select被選中項的文字

var item = $("select[name=items] option[selected]").text();

3.select下拉框的第二個元素為當前選中值

$('#select_id')[0].selectedindex = 1;

4.radio單選組的第二個元素為當前選中值

$('input[name=items]').get(1).checked = true;

七、獲取值

文字框,文字區域:$("#txt").attr("value"); 

多選框 checkbox:$("#checkbox_id").attr("value");

單選組radio: $("input[type=radio][checked]").val();

下拉框select: $('#sel').val();

八、控制表單元素

文字框,文字區域:$(

"#txt").attr("value",'');//清空內容

$("#txt").attr("value",'11');//填充內容

多選框checkbox: $(

"#chk1").attr("checked",'');//不打勾

$("#chk2").attr("checked",true);//打勾

if($(

"#chk1").attr('checked')==undefined) //判斷是否已經打勾

單選組 radio://設定value=2的專案為當前選中項 $(

"input[type=radio]").attr("checked",'2');

下拉框 select://設定value=-sel3的專案為當前選中項 $(

"#sel").attr("value",'-sel3');

//新增下拉框的option

$("1111

//清空下拉框

$("#sel").empty();

使用jquery操作select

jquery獲取select選擇的text和value 語法解釋 1.select id change function 為select新增事件,當選擇其中一項時觸發 2.var checktext select id find option selected text 獲取select選擇的tex...

jquery操作select大全

jquery獲取select選擇的text和value 1.select id change function 為select新增事件,當選擇其中一項時觸發 2.var checktext select id find option selected text 獲取select選擇的text 3.v...

jquery操作 select 物件

jquery select取值,賦值操作 一 獲取select 獲取select 選中的 text ddlregtype find option selected text 獲取select選中的索引 ddlregtype get 0 selectedindex 二 設定select 設定selec...