Jquery操作下拉框(DropDownList)實(shí)現(xiàn)取值賦值
更新時(shí)間:2013年08月13日 16:22:28 作者:
Jquery操作下拉框(DropDownList)想必大家都有所接觸吧,下面與大家分享下對DropDownList進(jìn)行取值賦值的實(shí)現(xiàn)代碼
1. 獲取選中項(xiàng):
獲取選中項(xiàng)的Value值:
$('select#sel option:selected').val();
或者
$('select#sel').find('option:selected').val();
獲取選中項(xiàng)的Text值:
$('select#seloption:selected').text();
或者
$('select#sel').find('option:selected').text();
2. 獲取當(dāng)前選中項(xiàng)的索引值:
$('select#sel').get(0).selectedIndex;
3. 獲取當(dāng)前option的最大索引值:
$('select#sel option:last').attr("index")
4. 獲取DropdownList的長度:
$('select#sel')[0].options.length;
或者
$('select#sel').get(0).options.length;
5. 設(shè)置第一個(gè)option為選中值:
$('select#sel option:first').attr('selected','true')
或者
$('select#sel')[0].selectedIndex = 0;
6. 設(shè)置最后一個(gè)option為選中值:
$('select#sel option:last).attr('selected','true')
7. 根據(jù)索引值設(shè)置任意一個(gè)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.刪除第幾個(gè)option:
$(" select#sel option ").eq(索引值).remove();索引值=0,1,2....
如刪除第3個(gè)Radio:
$(" select#sel option ").eq(2).remove();
11.刪除第一個(gè)option:
$(" select#sel option ").eq(0).remove();
或者
$("select#sel option:first").remove();
12. 刪除最后一個(gè)option:
$("select#sel option:last").remove();
13. 刪除dropdownlist:
$("select#sel").remove();
14.在select后面添加一個(gè)option:
$("select#sel").append("f");
15. 在select前面添加一個(gè)option:
$("select#sel").prepend("0");
16. 遍歷option:
$(' select#sel option ').each(function (index, domEle) {
//寫入代碼
});
復(fù)制代碼 代碼如下:
獲取選中項(xiàng)的Value值:
$('select#sel option:selected').val();
或者
$('select#sel').find('option:selected').val();
獲取選中項(xiàng)的Text值:
$('select#seloption:selected').text();
或者
$('select#sel').find('option:selected').text();
2. 獲取當(dāng)前選中項(xiàng)的索引值:
復(fù)制代碼 代碼如下:
$('select#sel').get(0).selectedIndex;
3. 獲取當(dāng)前option的最大索引值:
復(fù)制代碼 代碼如下:
$('select#sel option:last').attr("index")
4. 獲取DropdownList的長度:
復(fù)制代碼 代碼如下:
$('select#sel')[0].options.length;
或者
$('select#sel').get(0).options.length;
5. 設(shè)置第一個(gè)option為選中值:
復(fù)制代碼 代碼如下:
$('select#sel option:first').attr('selected','true')
或者
$('select#sel')[0].selectedIndex = 0;
6. 設(shè)置最后一個(gè)option為選中值:
復(fù)制代碼 代碼如下:
$('select#sel option:last).attr('selected','true')
7. 根據(jù)索引值設(shè)置任意一個(gè)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.刪除第幾個(gè)option:
復(fù)制代碼 代碼如下:
$(" select#sel option ").eq(索引值).remove();索引值=0,1,2....
如刪除第3個(gè)Radio:
$(" select#sel option ").eq(2).remove();
11.刪除第一個(gè)option:
復(fù)制代碼 代碼如下:
$(" select#sel option ").eq(0).remove();
或者
$("select#sel option:first").remove();
12. 刪除最后一個(gè)option:
復(fù)制代碼 代碼如下:
$("select#sel option:last").remove();
13. 刪除dropdownlist:
復(fù)制代碼 代碼如下:
$("select#sel").remove();
14.在select后面添加一個(gè)option:
復(fù)制代碼 代碼如下:
$("select#sel").append("f");
15. 在select前面添加一個(gè)option:
復(fù)制代碼 代碼如下:
$("select#sel").prepend("0");
16. 遍歷option:
復(fù)制代碼 代碼如下:
$(' select#sel option ').each(function (index, domEle) {
//寫入代碼
});
您可能感興趣的文章:
- jQuery操作select下拉框的text值和value值的方法
- jquery multiSelect 多選下拉框
- 基于jQuery的select下拉框選擇觸發(fā)事件實(shí)例分析
- jquery及原生js獲取select下拉框選中的值示例
- jquery獲得下拉框值的代碼
- jquery動態(tài)加載select下拉框示例代碼
- jQuery+easyui中的combobox實(shí)現(xiàn)下拉框特效
- jQuery實(shí)現(xiàn)監(jiān)聽下拉框選中內(nèi)容發(fā)生改變操作示例
- JQuery實(shí)現(xiàn)級聯(lián)下拉框效果實(shí)例講解
- jquery實(shí)現(xiàn)戶籍地選擇下拉框
相關(guān)文章
Bootstrap框架建立樹形菜單(Tree)的實(shí)例代碼
這篇文章主要介紹了在Bootstrap框架下怎么去建立一個(gè)樹形菜單,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2017-10-10
jquery處理checkbox(復(fù)選框)是否被選中實(shí)例代碼
這篇文章主要介紹了jquery處理checkbox(復(fù)選框)是否被選中實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-06-06
基于jQuery Easyui實(shí)現(xiàn)登陸框界面
本文通過實(shí)例代碼給大家分享了基于jQuery Easyui實(shí)現(xiàn)登陸框界面,代碼簡單易懂,非常不錯,具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-07-07
jQuery/JS監(jiān)聽input輸入框值變化實(shí)例
在本篇文章里小編給大家整理的是一篇關(guān)于jQuery/JS監(jiān)聽input輸入框值變化實(shí)例內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-10-10
jQuery實(shí)現(xiàn)的一個(gè)自定義Placeholder屬性插件
這篇文章主要介紹了jQuery實(shí)現(xiàn)的一個(gè)自定義Placeholder屬性插件,本文最后附有插件完整源碼,需要的朋友可以參考下2014-08-08

