浅析JQuery获取和设置Select选项的常用方法总结
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项的个数
$("#cuschildtypeid").get(0).options.length
5.设置select 选中的索引:
$("#cuschildtypeid").get(0).selectedindex=index;//index为索引值
6.设置select 选中的value:
$("#cuschildtypeid").attr("value","normal");
$("#cuschildtypeid").val("normal");
$("#cuschildtypeid").get(0).value = "normal";
7.设置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中添加一项,显示内容为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;