获取select
先看看下面代码:
复制代码 代码如下:
$(#select_id).change(function(){//code...}); //为select添加事件,当选择其中一项时触发
var checktext=$(#select_id).find(option:selected).text(); //获取select选择的text
var checkvalue=$(#select_id).val(); //获取select选择的value
var checkindex=$(#select_id ).get(0).selectedindex; //获取select选择的索引值
var maxindex=$(#select_id option:last).attr(index); //获取select最大的索引值
$(#select_id ).get(0).selectedindex=1; //设置select索引值为1的项选中
$(#select_id ).val(4); //设置select的value值为4的项选中
$(#select_id option[text='jquery']).attr(selected, true); //设置select的text值为jquery的项选中
获取select 选中的 text :
复制代码 代码如下:
$(#ddlregtype).find(option:selected).text();
获取select选中的 value:
复制代码 代码如下:
$(#nowamagic).val();
获取select选中的索引:
复制代码 代码如下:
$(#nowamagic).get(0).selectedindex;
设置select
jquery添加/删除select的option项:
复制代码 代码如下:
$(#select_id).append(text); //为select追加一个option(下拉项)
$(#select_id).prepend(请选择); //为select插入一个option(第一个位置)
$(#select_id option:last).remove(); //删除select中索引值最大option(最后一个)
$(#select_id option[index='0']).remove(); //删除select中索引值为0的option(第一个)
$(#select_id option[value='3']).remove(); //删除select中value='3'的option
$(#select_id option[text='4']).remove(); //删除select中text='4'的option
设置select 选中的索引:
复制代码 代码如下:
//index为索引值
$(#nowamagic).get(0).selectedindex=index;
设置select 选中的value:
复制代码 代码如下:
$(#nowamagic).attr(value,normal);
$(#nowamagic).val(normal);
$(#nowamagic).get(0).value = value;
设置select 选中的text:
复制代码 代码如下:
var count=$(#nowamagicoption).length;
for(var i=0;i{ if($(#nowamagic).get(0).options[i].text == text)
{
$(#nowamagic).get(0).options[i].selected = true;
break;
}
}
清空 select:
复制代码 代码如下:
$(#nowamagic).empty();