本文主要整理部分jQuery操作select相关的api
?
1、获取select选中的text
?
?
$('#selectID').find("option:selected").text();
?
2、获取select选中的value
?
?
$('#selectID').val(); //和一般的一样
?
3、获取被选中的索引
?
?
$('#selectID').get(0).selectedIndex;
$('#selectID')[0].selectedIndex;
?
4、设置select的选中的value
?
$('#selectID').val('1');
$('#selectID').attr('value',1);
$('#selectID')[0].value =1;
$('#selectID').get(0).value = 1;
?
?
?
5、清空select
?
?
$('#selectID').empty();
?
6、操作option
?
- 在已有的option的最前面添加一个新的
$('#selectID').prepend("<option value='0'>请选择</option>");
?
- 在已有的option的最后添加一个新的
$('#selectID').append("<option value='10'>测试</option>");
?
- 通过索引来获取对应option
//option的index也是从0开始的
$('#selectID option[index=1]');
?