JQuery給元素添加/刪除節(jié)點(diǎn)比如select
更新時間:2013年04月02日 15:43:37 作者:
本教程詳細(xì)介紹下jQuery添加/刪除Select的Option項(xiàng),感興趣的各位可以參考下哈
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項(xiàng):
jQuery("#select_id").append("<option value='Value'>Text</option>"); //為Select追加一個Option(下拉項(xiàng))
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項(xiàng):
復(fù)制代碼 代碼如下:
jQuery("#select_id").append("<option value='Value'>Text</option>"); //為Select追加一個Option(下拉項(xiàng))
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)文章
文本溢出插件jquery.dotdotdot.js使用方法詳解
這篇文章主要介紹了文本溢出插件jquery.dotdotdot.js使用方法詳解,需要的朋友可以參考下2017-06-06
基于jQuery實(shí)現(xiàn)點(diǎn)擊彈出層實(shí)例代碼
基于jquery實(shí)現(xiàn)點(diǎn)擊鏈接彈出層效果,其基本實(shí)現(xiàn)原理是這樣的:jquery操作DOM元素。對層樣式的設(shè)置。將display:設(shè)置為none;讓層隱藏;對jq點(diǎn)擊彈出層相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2016-01-01
EasyUI中實(shí)現(xiàn)form表單提交的示例分享
這里給大家分享的是一段使用EasyUI中實(shí)現(xiàn)form表單提交的方法的核心代碼,小伙伴們根據(jù)自己的需求補(bǔ)全form部分吧,希望大家能夠喜歡。2015-03-03
Jquery鼠標(biāo)放上去顯示全名的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狫query鼠標(biāo)放上去顯示全名的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
jQuery動態(tài)設(shè)置form表單的enctype值(實(shí)現(xiàn)代碼)
本篇文章是對在jQuery中動態(tài)設(shè)置form表單的enctype值的實(shí)現(xiàn)代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07
jQuery選中select控件 無法設(shè)置selected的解決方法
select 控件的 option用jQuery動態(tài)添加,然后選中某項(xiàng)時,IE6不能執(zhí)行(火狐沒問題),用try{}catch(err){alert(err.description);}提示為“無法設(shè)置selected屬性 未指明的錯誤”2010-09-09

