JS设置select下拉框默认选中
JS设置select下拉框默认选中。
文章共687字 · 阅读需要大约3分钟
一键AI生成摘要,助你高效阅读
问答
·
<select id="selectExample">
<option value="1">aaa</option>
<option value="2">bbb</option>
<option value="3">ccc</option>
</select>
- 根据value值设置默认选中
//方法一:
$("#selectExample").val('1');//设置value为1的option选项为默认选中
//方法二:
$("#selectExample option[value='1']").prop("selected",true);
//方法三:
$("#selectExample option[value='1']").prop("selected",selected);
- 根据text值设置默认选中
var selected="上海";
$("#selectExample").find("option").each(function(){
if($(this).text() == selected) {
$(this).attr("selected",true);
}
});
多选框默认选中
$("#sel").val(Array('1','2'));//设置value=1和2的选项为默认选中
更多推荐
已为社区贡献16条内容
所有评论(0)