本文實例講述了jQuery增加、刪除及修改select option的方法。分享給大家供大家參考,具體如下:
jQuery獲取Select選擇的Text和Value:
1.
//獲取Select選擇的Textvar checkText=jQuery("#select_id").find("option:selected").text(); 2.
//獲取Select選擇的option Valuevar checkValue=jQuery("#select_id").val();3.
//獲取Select選擇的索引值var checkIndex=jQuery("#select_id ").get(0).selectedIndex;4.
//獲取Select最大的索引值var maxIndex=jQuery("#select_id option:last").attr("index");jQuery添加/刪除Select的Option項:
1.
//為Select追加一個Option(下拉項)jQuery("#select_id").append("<option value='Value'>Text</option>");2.
//為Select插入一個Option(第一個位置)jQuery("#select_id").prepend("<option value='0'>請選擇</option>");3.
//刪除Select中索引值最大Option(最后一個)jQuery("#select_id option:last").remove();4.
//刪除Select中索引值為0的Option(第一個)jQuery("#select_id option[index='0']").remove();5.
//刪除Select中Value='3'的OptionjQuery("#select_id option[value='3']").remove();6.
//刪除Select中Text='4'的OptionjQuery("#select_id option[text='4']").remove();內容清空:
jQuery("#select_id").empty();更多關于jQuery相關內容感興趣的讀者可查看本站專題:《jQuery form操作技巧匯總》、《jQuery切換特效與技巧總結》、《jQuery拖拽特效與技巧總結》、《jQuery擴展技巧總結》、《jQuery常見經典特效匯總》、《jQuery動畫與特效用法總結》、《jquery選擇器用法總結》及《jQuery常用插件及用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
新聞熱點
疑難解答