淺析JQuery獲取和設(shè)置Select選項的常用方法總結(jié)
1.獲取select 選中的 text:
$("#cusChildTypeId").find("option:selected").text();
$("#cusChildTypeId option:selected").text()
2.獲取select選中的 value:
$("#ddlRegType ").val();
3.獲取select選中的索引:
$("#ddlRegType ").get(0).selectedIndex;
4.得到select項的個數(shù)
$("#cusChildTypeId").get(0).options.length
5.設(shè)置select 選中的索引:
$("#cusChildTypeId").get(0).selectedIndex=index;//index為索引值
6.設(shè)置select 選中的value:
$("#cusChildTypeId").attr("value","Normal");
$("#cusChildTypeId").val("Normal");
$("#cusChildTypeId").get(0).value = "Normal";
7.設(shè)置select 選中的text:
1>.var count=$("#cusChildTypeId").get(0).options.length;
for(var i=0;i<count;i++)
{
if($("#cusChildTypeId").get(0).options.text == text)
{
$("#cusChildTypeId").get(0).options.selected = true;
break;
}
}
2>.$("#cusChildTypeId").val(text);
$("#cusChildTypeId").change();
8.向select中添加一項,顯示內(nèi)容為text,值為value
$("#cusChildTypeId").get(0).options.add(new Option(text,value));
9.刪除select中值為value的項
var count = $("#cusChildTypeId").size();
for(var i=0;i<count;i++)
{
if($("#cusChildTypeId").get(0).options[i].value == value)
{
$("#cusChildTypeId").get(0).remove(i);
break;
}
}
10.清空 Select:
1>. $("#cusChildTypeId").empty();
2>. $("#cusChildTypeId").get(0).options.length = 0;
相關(guān)文章
jQuery下實現(xiàn)等待指定元素加載完畢(可改成純js版)
下面就是我想到的等待元素出現(xiàn)方法,雖然是基于jQuery的,但是代碼很簡潔,可以修改成純js版的,感興趣的朋友可以參考下哈,希望對大家有所幫助2013-07-07
jquery 獲取標(biāo)簽名(tagName)示例代碼
如果是需要取到tagName后再進(jìn)行判斷,那么下面的代碼或許對大家有所幫助,感興趣的朋友不妨嘗試一下哈2013-07-07
關(guān)于jquery中attr()和prop()方法的區(qū)別
今兒是腳本之家小編給大家總結(jié)的jquery中attr()和prop()方法的區(qū)別,感興趣的朋友參考下2018-05-05
jQuery CSS3自定義美化Checkbox實現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了jQuery CSS3自定義美化Checkbox實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05
jquery實現(xiàn)仿Flash的橫向滑動菜單效果代碼
這篇文章主要介紹了jquery實現(xiàn)仿Flash的橫向滑動菜單效果代碼,可實現(xiàn)滑塊跟隨鼠標(biāo)滑動效果的功能,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09

