- 时间:2021-03-31 22:17 编辑: 来源: 阅读:
- 扫一扫,手机访问
摘要:浅析JQuery获取和设置Select选项的常用方法总结
[b]1.获取select 选中的 text:
[/b] $("#cusChildTypeId").find("option:selected").text();
$("#cusChildTypeId option:selected").text()
[b]2.获取select选中的 value:
[/b] $("#ddlRegType ").val();
[b]
3.获取select选中的索引:
[/b] $("#ddlRegType ").get(0).selectedIndex;
[b]
4.得到select项的个数[/b]
$("#cusChildTypeId").get(0).options.length
[b]
5.设置select 选中的索引:
[/b] $("#cusChildTypeId").get(0).selectedIndex=index;//index为索引值
[b]
6.设置select 选中的value:
[/b] $("#cusChildTypeId").attr("value","Normal");
$("#cusChildTypeId").val("Normal");
$("#cusChildTypeId").get(0).value = "Normal";
[b]
7.设置select 选中的text:
[/b] 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();
[b]
8.向select中添加一项,显示内容为text,值为value[/b]
$("#cusChildTypeId").get(0).options.add(new Option(text,value));
[b]
9.删除select中值为value的项
[/b] 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;
}
}
[b]
10.清空 Select:
[/b] 1>. $("#cusChildTypeId").empty();
2>. $("#cusChildTypeId").get(0).options.length = 0;