国产探花免费观看_亚洲丰满少妇自慰呻吟_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');//追加給對方            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 南通市| 平湖市| 临江市| 习水县| 天全县| 阜新市| 太白县| 馆陶县| 成武县| 台山市| 泸定县| 探索| 涞水县| 盘锦市| 三明市| 衡阳县| 云安县| 金沙县| 咸宁市| 桐乡市| 宜宾市| 四平市| 聂拉木县| 息烽县| 海城市| 仙桃市| 荥经县| 永寿县| 佛教| 鄂托克前旗| 敦化市| 平南县| 石棉县| 泗水县| 永春县| 祁门县| 韩城市| 依安县| 伊宁县| 顺昌县| 个旧市|