JQuery获取和设置Select选项方法汇总如下:
获取select
先看看下面代码:
1 |
$("#select_id").change(function(){ |
2 |
var checkText=$("#select_id").find("option:selected").text(); |
3 |
var checkValue=$("#select_id").val(); |
4 |
var checkIndex=$("#select_id ").get(0).selectedIndex; |
5 |
var maxIndex=$("#select_id option:last").attr("index"); |
6 |
$("#select_id ").get(0).selectedIndex=1; |
7 |
$("#select_id ").val(4); |
8 |
$("#select_id option[text='jQuery']").attr("selected", true); |
获取select 选中的 text :
1 |
$("#ddlRegType").find("option:selected").text(); |
获取select选中的 value:
获取select选中的索引:
1 |
$("#nowamagic").get(0).selectedIndex; |
设置select
jQuery添加/删除Select的Option项:
1 |
$("#select_id").append("<option value='Value'>Text</option>"); |
2 |
$("#select_id").prepend("<option value='0'>请选择</option>"); |
3 |
$("#select_id option:last").remove(); |
4 |
$("#select_id option[index='0']").remove(); |
5 |
$("#select_id option[value='3']").remove(); |
6 |
$("#select_id option[text='4']").remove(); |
设置select 选中的索引:
2 |
$("#nowamagic").get(0).selectedIndex=index; |
设置select 选中的value:
1 |
$("#nowamagic").attr("value","Normal"); |
2 |
$("#nowamagic").val("Normal"); |
3 |
$("#nowamagic").get(0).value = value; |
设置select 选中的text:
1 |
var count=$("#nowamagicoption").length; |
2 |
for(var i=0;i<count;i++) |
3 |
{ if($("#nowamagic").get(0).options[i].text == text) |
5 |
$("#nowamagic").get(0).options[i].selected = true; |
清空 select:
1 |
$("#nowamagic").empty(); |