国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

jquery操作select常見方法大全【7種情況】

2024-05-06 15:38:50
字體:
來源:轉載
供稿:網友

本文實例講述了jquery操作select常見方法。分享給大家供大家參考,具體如下:

在前段HTML頁面設計中select 下拉框,或者 在 multiple="multiple" 時,表現為列表。經常會在頁面上對其進行操作,這些操作不外乎:

1. 得到選中的 select 的 option 的值或者text.
2. 刪除選中的 select 的 option.
3. 向select中增加新的option.
4. 得到select option 長度,也就是個數size
5. 清空select.
6. 兩個select 框之間互相添加刪除,從左邊到右邊,從右邊到左邊的操作,通常是多選情況。
7. 判斷在 select 框中是否存在某一個值的選項

對第一種情況,用如下方法:

$("#select_id").change(function(){//code...});  //為Select添加事件,當選擇其中一項時觸發var checkText=$("#select_id").find("option:selected").text();  //獲取Select選擇的Textvar checkValue=$("#select_id").val();  //獲取Select選擇的Valuevar checkIndex=$("#select_id ").get(0).selectedIndex;  //獲取Select選擇的索引值var maxIndex=$("#select_id option:last").attr("index");  //獲取Select最大的索引值 jQuery設置Select選擇的Text和Value:
$("#select_id ").get(0).selectedIndex=1;  //設置Select索引值為1的項選中$("#select_id ").val(4);  //設置Select的Value值為4的項選中$("#select_id option[text='jQuery']").attr("selected", true);  //設置Select的Text值為jQuery的項選中

對第二種情況,刪除的處理:

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

如果要刪除選中的option ,則需要先得到 選中option 的序號. var checkIndex=$("#select_id ").get(0).selectedIndex; 然后再調用上面的方法刪除.

對第三種情況,增加option 的處理:

$("#select_id").append("<option value='Value'>Text</option>");  //為Select追加一個Option(下拉項)$("#select_id").prepend("<option value='0'>請選擇</option>");  //為Select插入一個Option(第一個位置)

對第四種情況,得到select 的長度

var totalcount=$("#single_user_choice").get(0).options.length;

第五種情況,清空select

$("#single_user_choice").get(0).options.length=0;

第六種情況。兩個select 框之間互相添加刪除,從左邊到右邊,從右邊到左邊的操作,通常是多選情況,也就是設置了 multiple="multiple" 。

var $options = $('#select1 option:selected');//獲取當前選中的項var $remove = $options.remove();//刪除下拉列表中選中的項$remove.appendTo('#select2');//追加給對方            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 梧州市| 青海省| 炎陵县| 丹江口市| 囊谦县| 朝阳市| 亳州市| 什邡市| 沈丘县| 新建县| 宁武县| 祁连县| 南通市| 达日县| 额尔古纳市| 南雄市| 新巴尔虎左旗| 延寿县| 习水县| 贵州省| 佛坪县| 海兴县| 韶山市| 孝昌县| 揭西县| 平武县| 尉氏县| 原阳县| 集安市| 遂平县| 清流县| 星座| 昌都县| 沐川县| 密山市| 泾阳县| 上虞市| 梧州市| 理塘县| 清河县| 舒兰市|