jQuery select操作控制方法

2021-06-22 10:20:26 字數 2189 閱讀 9702

看了一下jquery的一些方法後,理出了一些常用的方法,列在下面: 

//獲取第乙個option的值 

$('#test option:first').val(); 

//最後乙個option的值 

$('#test option:last').val(); 

//獲取第二個option的值 

$('#test option:eq(1)').val(); 

//獲取選中的值 

$('#test').val(); 

$('#test option:selected').val(); 

//設定值為2的option為選中狀態 

$('#test').attr('value','2'); 

//設定第乙個option為選中 

$('#test option:last').attr('selected','selected'); 

$("#test").attr('value' , $('#test option:last').val()); 

$("#test").attr('value' , $('#test option').eq($('#test option').length - 1).val()); 

//獲取select的長度 

$('#test option').length; 

//新增乙個option 

//添除選中項 

$('#test option:selected').remove(); 

//指定項選中 

$('#test option:first').remove(); 

//指定值被刪除 

$('#test option').each(function() 

}); 

$('#test option[value=5]').remove(); 

//獲取第乙個group的標籤 

$('#test optgroup:eq(0)').attr('label'); 

//獲取第二group下面第乙個option的值 

$('#test optgroup:eq(1) :option:eq(0)').val(); 

獲取select中選擇的text與value相關的值

獲取select選擇的text : var checktext=$("#slc1").find("option:selected").text(); 

獲取select選擇的value:var checkvalue=$("#slc1").val(); 

獲取select選擇的索引值: var checkindex=$("#slc1 ").get(0).selectedindex; 

獲取select最大的索引值: var maxindex=$("#slc1 option:last").attr("index"); 

設定select選擇的text和value

設定select索引值為1的項選中:$("#slc1 ").get(0).selectedindex=1; 

設定select的value值為4的項選中: $("#slc1 ").val(4); 

設定select的text值為jquery的選中: 

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

ps:特別要注意一下第三項的使用哦。看看jquery的選擇器功能是如此地強大呀! 

新增刪除option項

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

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

$("#slc2").prepend("請選擇"); 

ps: prepend 這是向所有匹配元素內部的開始處插入內容的最佳方式。 

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

$("#slc2 option:last").remove(); 

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

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

刪除select中value='3'的option 

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

刪除select中text='4'的option 

$("#slc2 option[text='3']").remove();

Jquery select 元素操作

設定值為0的option 移除 id option value 0 remove 設定索引值為0的option選中 id get 0 selectedindex 0 獲取select 選中的 text ddlregtype find option selected text 獲取select選中的 ...

jquery select 相關操作

每一次操作select的時候,總是要出來翻一下資料,不如自己總結一下,以後就翻這裡了。比如 1 設定value為pxx的項選中 selector val pxx 2 設定text為pxx的項選中 selector find option text pxx attr selected true 這裡有...

jQuery Select的操作集合

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