1.为了统一样式,常常要模拟placeholder
1 <select> 2 3 <option disabled selected hidden>请选择</option> 4 5 <option>what</option> 6 7 <option>the</option> 8 9 <option>hell</option> 10 11 </select>
2.select选中的值和某个值要被选中(ios兼容)
1 <select style="font-size: 60px"> 2 <option>2</option> 3 <option>5年</option> 4 <option>10年</option> 5 <option>20年</option> 6 </select> 7 <select style="font-size: 50px" id="select_id "> 8 <option value="1">年交</option> 9 <option value="2">趸交</option> 10 </select>
a 取得选中值:
获取select 选中的 text :
$("selecr").eq(0).find("option:selected").text();
获取select选中的 value:
$("selecr").eq(0).val();
获取select选中的索引:
$("selecr")[0].selectedindex;
b 设置select
var iText= $("selecr").eq(0).find("option:selected").text();
$('select').eq(1).find("option[value = '"+iText+"']").prop("selected","selected");
之前用的是attr,但是发现ios怎么都不行,没想到只要把attr换成prop就没有兼容问题了,ios真是磨人的小妖精啊!
还有$('select').eq(1).val(iText);
$('select').eq(1).get(0).value =iText;
下面这俩种没有做兼容测试,按道理是没有问题的因为是修改之前的代码,不敢大改
c 清空 select:
$('select').eq(1).empty();
d 增加或减少select的opacity项
1 $'(#select_id').append("<option value='value'>text</option>"); //添加一项option 2 $(#select_id').prepend("<option value='0'>请选择</option>"); //在前面插入一项option 3 $("#select_id option:last").remove(); //删除索引值最大的option 4 $("#select_id option[index='0']").remove();//删除索引值为0的option 5 $("#select_id option[value='3']").remove(); //删除值为3的option 6 $("#select_id option[text='4']").remove(); //删除text值为4的option