JQuery中對Select的option項(xiàng)的添加、刪除、取值
更新時(shí)間:2013年08月25日 12:27:56 作者:
jQuery獲取Select選擇的Text和Value及添加/刪除Select的Option項(xiàng),在本文將為大家詳細(xì)介紹下具體的實(shí)現(xiàn),感興趣的朋友可以參考下
jQuery獲取Select選擇的Text和Value:
$("#select_id").change(function(){//code...}); //為Select添加事件,當(dāng)選擇其中一項(xiàng)時(shí)觸發(fā)
var checkText=$("#select_id").find("option:selected").text(); //獲取Select選擇的Text
var checkValue=$("#select_id").val(); //獲取Select選擇的Value
var checkIndex=$("#select_id ").get(0).selectedIndex; //獲取Select選擇的索引值
var maxIndex=$("#select_id option:last").attr("index"); //獲取Select最大的索引值
jQuery添加/刪除Select的Option項(xiàng):
$("#select_id").append("<option value='Value'>Text</option>"); //為Select追加一個(gè)Option(下拉項(xiàng))
$("#select_id").prepend("<option value='0'>請選擇</option>"); //為Select插入一個(gè)Option(第一個(gè)位置)
$("#select_id option:last").remove(); //刪除Select中索引值最大Option(最后一個(gè))
$("#select_id option[index='0']").remove(); //刪除Select中索引值為0的Option(第一個(gè))
$("#select_id option[value='3']").remove(); //刪除Select中Value='3'的Option
$("#select_id option[text='4']").remove(); //刪除Select中Text='4'的Option
內(nèi)容清空:
$("#charCity").empty();
復(fù)制代碼 代碼如下:
$("#select_id").change(function(){//code...}); //為Select添加事件,當(dāng)選擇其中一項(xiàng)時(shí)觸發(fā)
var checkText=$("#select_id").find("option:selected").text(); //獲取Select選擇的Text
var checkValue=$("#select_id").val(); //獲取Select選擇的Value
var checkIndex=$("#select_id ").get(0).selectedIndex; //獲取Select選擇的索引值
var maxIndex=$("#select_id option:last").attr("index"); //獲取Select最大的索引值
jQuery添加/刪除Select的Option項(xiàng):
復(fù)制代碼 代碼如下:
$("#select_id").append("<option value='Value'>Text</option>"); //為Select追加一個(gè)Option(下拉項(xiàng))
$("#select_id").prepend("<option value='0'>請選擇</option>"); //為Select插入一個(gè)Option(第一個(gè)位置)
$("#select_id option:last").remove(); //刪除Select中索引值最大Option(最后一個(gè))
$("#select_id option[index='0']").remove(); //刪除Select中索引值為0的Option(第一個(gè))
$("#select_id option[value='3']").remove(); //刪除Select中Value='3'的Option
$("#select_id option[text='4']").remove(); //刪除Select中Text='4'的Option
內(nèi)容清空:
復(fù)制代碼 代碼如下:
$("#charCity").empty();
相關(guān)文章
利用JQuery操作iframe父頁面、子頁面的元素和方法匯總
這篇文章主要給大家介紹了關(guān)于利用JQuery操作iframe父頁面、子頁面的元素和方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09jQuery實(shí)現(xiàn)只允許輸入數(shù)字和小數(shù)點(diǎn)的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)只允許輸入數(shù)字和小數(shù)點(diǎn)的方法,涉及jQuery針對鍵盤事件的響應(yīng)及字符串操作的相關(guān)技巧,需要的朋友可以參考下2016-03-03jquery siblings獲取同輩元素用法實(shí)例分析
這篇文章主要介紹了jquery siblings獲取同輩元素用法,結(jié)合實(shí)例形式分析了jQuery使用siblings遍歷同級元素的相關(guān)技巧,需要的朋友可以參考下2016-07-07