天天看點

Jquery操作下拉框(DropDownList)實作取值指派

Jquery操作下拉框(DropDownList)想必大家都有所接觸吧,下面與大家分享下對DropDownList進行取值指派的實作代碼

1. 擷取選中項:

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. 設定第一個option為選中值:

$('select#sel option:first').attr('selected','true')

$('select#sel')[0].selectedIndex = 0;

6. 設定最後一個option為選中值:

$('select#sel option:last).attr('selected','true')

7. 根據索引值設定任意一個option為選中值:

$('select#sel')[0].selectedIndex =索引值;索引值=0,1,2....

8. 設定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) {

//寫入代碼

});

原文:http://www.jb51.net/article/40545.htm

繼續閱讀