您好,欢迎来到达界科技。
搜索
您的当前位置:首页jquery如何让select选中某个选项的实例详解

jquery如何让select选中某个选项的实例详解

来源:达界科技


jquery如何让select选中某个选项的实例详解

<span id="xx">广州市</span>
<select id="select_id"> 
<option>北京市</option> 
<option>广州市</option> 
<option>上海市</option>
</select>

让select 选中 广州市 ?
错误用法:

$("#select_id option[text='广州市']").attr("selected", true); 
<script>
//普通js写法
document.getElementById("select_id").options[1].setAttribute("selected", true);
//jquery应该这样写
$("#select_id option").eq(1).attr("selected",true);
</script>
<script>
 $("#select_id option[value='广州市']").attr("selected", true);
</script>
$("#select_id option[text='广州市']").attr("selected", "selected");


正确用法:

 $("#select_id")[0].selectedIndex = 1;

jquery如何设置select索引选中,直接用dom操作方式操作索引:

$('#selectId').get(0).selectedIndex=index;

用值方式操作索引:

$('#selectId').val('selectIndexValue');


扩展:
获取当前选中项的value

$("#select_id").val();

获取当前选中项的text

$("#select_id").find("option:selected").text();

Copyright © 2019- dajiepai.com 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务