Jquery操作下拉框(DropDownList)實現(xiàn)取值賦值
更新時間:2013年08月13日 16:22:28 作者:
Jquery操作下拉框(DropDownList)想必大家都有所接觸吧,下面與大家分享下對DropDownList進行取值賦值的實現(xiàn)代碼
1. 獲取選中項:
獲取選中項的Value值:
$('select#sel option:selected').val();
或者
$('select#sel').find('option:selected').val();
獲取選中項的Text值:
$('select#seloption:selected').text();
或者
$('select#sel').find('option:selected').text();
2. 獲取當前選中項的索引值:
$('select#sel').get(0).selectedIndex;
3. 獲取當前option的最大索引值:
$('select#sel option:last').attr("index")
4. 獲取DropdownList的長度:
$('select#sel')[0].options.length;
或者
$('select#sel').get(0).options.length;
5. 設(shè)置第一個option為選中值:
$('select#sel option:first').attr('selected','true')
或者
$('select#sel')[0].selectedIndex = 0;
6. 設(shè)置最后一個option為選中值:
$('select#sel option:last).attr('selected','true')
7. 根據(jù)索引值設(shè)置任意一個option為選中值:
$('select#sel')[0].selectedIndex =索引值;索引值=0,1,2....
8. 設(shè)置Value=4 的option為選中值:
$('select#sel').attr('value','4');
或者
$("select#sel option[value='4']").attr('selected', 'true');
9. 刪除Value=3的option:
$("select#sel option[value='3']").remove();
10.刪除第幾個option:
$(" select#sel option ").eq(索引值).remove();索引值=0,1,2....
如刪除第3個Radio:
$(" select#sel option ").eq(2).remove();
11.刪除第一個option:
$(" select#sel option ").eq(0).remove();
或者
$("select#sel option:first").remove();
12. 刪除最后一個option:
$("select#sel option:last").remove();
13. 刪除dropdownlist:
$("select#sel").remove();
14.在select后面添加一個option:
$("select#sel").append("f");
15. 在select前面添加一個option:
$("select#sel").prepend("0");
16. 遍歷option:
$(' select#sel option ').each(function (index, domEle) {
//寫入代碼
});
復(fù)制代碼 代碼如下:
獲取選中項的Value值:
$('select#sel option:selected').val();
或者
$('select#sel').find('option:selected').val();
獲取選中項的Text值:
$('select#seloption:selected').text();
或者
$('select#sel').find('option:selected').text();
2. 獲取當前選中項的索引值:
復(fù)制代碼 代碼如下:
$('select#sel').get(0).selectedIndex;
3. 獲取當前option的最大索引值:
復(fù)制代碼 代碼如下:
$('select#sel option:last').attr("index")
4. 獲取DropdownList的長度:
復(fù)制代碼 代碼如下:
$('select#sel')[0].options.length;
或者
$('select#sel').get(0).options.length;
5. 設(shè)置第一個option為選中值:
復(fù)制代碼 代碼如下:
$('select#sel option:first').attr('selected','true')
或者
$('select#sel')[0].selectedIndex = 0;
6. 設(shè)置最后一個option為選中值:
復(fù)制代碼 代碼如下:
$('select#sel option:last).attr('selected','true')
7. 根據(jù)索引值設(shè)置任意一個option為選中值:
復(fù)制代碼 代碼如下:
$('select#sel')[0].selectedIndex =索引值;索引值=0,1,2....
8. 設(shè)置Value=4 的option為選中值:
復(fù)制代碼 代碼如下:
$('select#sel').attr('value','4');
或者
$("select#sel option[value='4']").attr('selected', 'true');
9. 刪除Value=3的option:
復(fù)制代碼 代碼如下:
$("select#sel option[value='3']").remove();
10.刪除第幾個option:
復(fù)制代碼 代碼如下:
$(" select#sel option ").eq(索引值).remove();索引值=0,1,2....
如刪除第3個Radio:
$(" select#sel option ").eq(2).remove();
11.刪除第一個option:
復(fù)制代碼 代碼如下:
$(" select#sel option ").eq(0).remove();
或者
$("select#sel option:first").remove();
12. 刪除最后一個option:
復(fù)制代碼 代碼如下:
$("select#sel option:last").remove();
13. 刪除dropdownlist:
復(fù)制代碼 代碼如下:
$("select#sel").remove();
14.在select后面添加一個option:
復(fù)制代碼 代碼如下:
$("select#sel").append("f");
15. 在select前面添加一個option:
復(fù)制代碼 代碼如下:
$("select#sel").prepend("0");
16. 遍歷option:
復(fù)制代碼 代碼如下:
$(' select#sel option ').each(function (index, domEle) {
//寫入代碼
});
您可能感興趣的文章:
相關(guān)文章
jquery的ajax如何使用ajaxSetup做全局請求攔截
在Web開發(fā)中,Ajax是一種常用的前后端數(shù)據(jù)交互技術(shù),由于業(yè)務(wù)需求的復(fù)雜性和安全性的考慮,我們可能需要對Ajax請求進行全局攔截和處理,以便統(tǒng)一處理一些共性問題,如權(quán)限驗證、錯誤處理等,本項目方案將介紹如何使用jQuery的Ajax實現(xiàn)全局請求攔截2023-11-11jquery next nextAll nextUntil siblings的區(qū)別介紹
在本文為大家詳細介紹下jquery next nextAll nextUntil siblings的區(qū)別,感興趣的朋友可以參考下2013-10-10jQuery實現(xiàn)的Email中的收件人效果(按del鍵刪除)
基于jquery實現(xiàn)的Email中的收件人效果,可通過del鍵刪除,需要的朋友可以參考下。2011-03-03