JQuery操作select

 

//爲Select添加事件,當選擇其中一項時觸發
 $("#select_id").change(function(){//code...}); 

//獲取Select選擇的Text
var checkText=$("#select_id").find("option:selected").text();  

//獲取Select選擇的Value
var checkValue=$("#select_id").val();   

//獲取Select選擇的索引值
var checkIndex=$("#select_id ").get(0).selectedIndex;   

 //獲取Select最大的索引值
var maxIndex=$("#select_id option:last").attr("index");  

//jQuery添加/刪除Select的Option項:

//爲Select追加一個Option(下拉項)
$("#select_id").append("<option value='Value'>Text</option>");   

//爲Select插入一個Option(第一個位置)
$("#select_id").prepend("<option value='0'>請選擇</option>");  

//刪除Select中索引值最大Option(最後一個)
$("#select_id option:last").remove(); 
  
//刪除Select中索引值爲0的Option(第一個)
$("#select_id option[index='0']").remove();   
 //刪除Select中Value='3'的Option
$("#select_id option[value='3']").remove();  
//刪除Select中Text='4'的Option
$("#select_id option[text='4']").remove();   

//內容清空:
$("#charCity").empty();
發佈了38 篇原創文章 · 獲贊 0 · 訪問量 2492
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章