JQuery給元素添加/刪除節(jié)點比如select
更新時間:2013年04月02日 15:43:37 作者:
本教程詳細介紹下jQuery添加/刪除Select的Option項,感興趣的各位可以參考下哈
jQuery獲取Select選擇的Text和Value:
var checkText=jQuery("#select_id").find("option:selected").text(); //獲取Select選擇的Text
var checkValue=jQuery("#select_id").val(); //獲取Select選擇的option Value
var checkIndex=jQuery("#select_id ").get(0).selectedIndex; //獲取Select選擇的索引值
var maxIndex=jQuery("#select_id option:last").attr("index"); //獲取Select最大的索引值
jQuery添加/刪除Select的Option項:
jQuery("#select_id").append("<option value='Value'>Text</option>"); //為Select追加一個Option(下拉項)
jQuery("#select_id").prepend("<option value='0'>請選擇</option>"); //為Select插入一個Option(第一個位置)
jQuery("#select_id option:last").remove(); //刪除Select中索引值最大Option(最后一個)
jQuery("#select_id option[index='0']").remove(); //刪除Select中索引值為0的Option(第一個)
jQuery("#select_id option[value='3']").remove(); //刪除Select中Value='3'的Option
jQuery("#select_id option[text='4']").remove(); //刪除Select中Text='4'的Option
內(nèi)容清空:
jQuery("#select_id").empty();
復(fù)制代碼 代碼如下:
var checkText=jQuery("#select_id").find("option:selected").text(); //獲取Select選擇的Text
var checkValue=jQuery("#select_id").val(); //獲取Select選擇的option Value
var checkIndex=jQuery("#select_id ").get(0).selectedIndex; //獲取Select選擇的索引值
var maxIndex=jQuery("#select_id option:last").attr("index"); //獲取Select最大的索引值
jQuery添加/刪除Select的Option項:
復(fù)制代碼 代碼如下:
jQuery("#select_id").append("<option value='Value'>Text</option>"); //為Select追加一個Option(下拉項)
jQuery("#select_id").prepend("<option value='0'>請選擇</option>"); //為Select插入一個Option(第一個位置)
jQuery("#select_id option:last").remove(); //刪除Select中索引值最大Option(最后一個)
jQuery("#select_id option[index='0']").remove(); //刪除Select中索引值為0的Option(第一個)
jQuery("#select_id option[value='3']").remove(); //刪除Select中Value='3'的Option
jQuery("#select_id option[text='4']").remove(); //刪除Select中Text='4'的Option
內(nèi)容清空:
復(fù)制代碼 代碼如下:
jQuery("#select_id").empty();
相關(guān)文章
簡單實現(xiàn)兼容各大瀏覽器的js復(fù)制內(nèi)容到剪切板
相信這個功能大家平時上網(wǎng)經(jīng)常能碰到,去看了幾個常用的網(wǎng)站,都是用的透明flash遮擋“復(fù)制到剪貼板”按鈕,所以當(dāng)你點擊按鈕的時候,點擊的其實是flash,然后把你需要復(fù)制的內(nèi)容傳入到了flash,最后通過flash的復(fù)制功能把傳入的內(nèi)容復(fù)制到了剪貼板。2015-09-09jQuery插件windowScroll實現(xiàn)單屏滾動特效
本文給大家分享的是一個使用jQuery插件windowScroll實現(xiàn)的單屏滾動的特效,主要參考搜狗瀏覽器4.2版本首頁的上下滾動效果。主要實現(xiàn)整個窗口的上下和左右滾動邏輯,非常的實用。2015-07-07jQuery實現(xiàn)自定義checkbox和radio樣式
這篇文章主要介紹了jQuery實現(xiàn)自定義checkbox和radio樣式的相關(guān)資料,需要的朋友可以參考下2015-07-07