jquery操作select(取值,設置選中)

最近工作中總出現select 和 option問題,整理一下,內容大部分源於網絡資料

一、基礎取值問題

例如

1、設置value爲pxx的項選中

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

2、設置text爲pxx的項選中

$(".selector").find("option:contains('pxx')").attr("selected",true);

注意:之前$(".selector").find("option[text='pxx']").attr("selected",true);這種寫法是錯誤的,目前個人證實input支持這種獲取屬性值的寫法:"input[text='pxx']",select中需要"option:contains('pxx')"這樣獲取。

(感謝博友@ sunnyjs 指正)

這裏有一箇中括號的用法,中括號裏的等號的前面是屬性名稱,不用加引號。很多時候,中括號的運用可以使得邏輯變得很簡單。

3、獲取當前選中項的value

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

4、獲取當前選中項的text

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

這裏用到了冒號,掌握它的用法並舉一反三也會讓代碼變得簡潔。

二、很多時候用到select的級聯,即第二個select的值隨着第一個select選中的值變化。這在jquery中是非常簡單的。

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

 // 先清空第二個

  $(".selector2").empty();

 // 實際的應用中,這裏的option一般都是用循環生成多個了

  var option = $("<option>").val(1).text("pxx");

  $(".selector2").append(option);

});

三、jQuery獲取Select選擇的Text和Value:

語法解釋:

  1. $("#select_id").change(function(){//code…}); //爲Select添加事件,當選擇其中一項時觸發
  2. var checkText=$("#select_id").find(“option:selected”).text(); //獲取Select選擇的Text
  3. var checkValue=$("#select_id").val(); //獲取Select選擇的Value
  4. var checkIndex=$("#select_id ").get(0).selectedIndex; //獲取Select選擇的索引值
  5. var maxIndex=$("#select_id option:last").attr(“index”); //獲取Select最大的索引值

四、jQuery設置Select選擇的 Text和Value:

語法解釋:

  1. $("#select_id ").get(0).selectedIndex=1; //設置Select索引值爲1的項選中
  2. $("#select_id ").val(4); // 設置Select的Value值爲4的項選中
  3. $("#select_id option[text=‘jQuery’]").attr(“selected”, true); //設置Select的Text值爲jQuery的項選中

五、jQuery添加/刪除Select的Option項:
語法解釋:

  1. $("#select_id").append(“Text”); //爲Select追加一個Option(下拉項)
  2. $("#select_id").prepend(“請選擇”); //爲Select插入一個Option(第一個位置)
  3. $("#select_id option:last").remove(); //刪除Select中索引值最大Option(最後一個)
  4. $("#select_id option[index=‘0’]").remove(); //刪除Select中索引值爲0的Option(第一個)
  5. $("#select_id option[value=‘3’]").remove(); //刪除Select中Value='3’的Option
  6. $("#select_id option[text=‘4’]").remove(); //刪除Select中Text='4’的Option

六、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;

七、獲取值:
文本框,文本區域:KaTeX parse error: Expected 'EOF', got '#' at position 3: ("#̲txt").attr("val…("#checkbox_id").attr(“value”);
單選組radio: $(“input[type=radio][checked]”).val();
下拉框select: $(’#sel’).val();

八、控制表單元素:
文本框,文本區域:$("#txt").attr(“value”,’’);//清空內容
$("#txt").attr(“value”,‘11’);//填充內容
多選框checkbox: $("#chk1").attr(“checked”,’’);//不打勾
KaTeX parse error: Expected 'EOF', got '#' at position 3: ("#̲chk2").attr("ch…("#chk1").attr(‘checked’)==undefined) //判斷是否已經打勾
單選組 radio: $(“input[type=radio]”).attr(“checked”,‘2’);//設置value=2的項目爲當前選中項
下拉框 select: $("#sel").attr(“value”,’-sel3’);//設置value=-sel3的項目爲當前選中項
$(“11112222”).appendTo("#sel")//添加下拉框的option
$("#sel").empty();//清空下拉框

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章