aaa
bbb
ccc
根据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);
}
});
备注(错误)
$("#selectExample").find("option[text='上海']").attr("selected",true);
要是想通过option的文本设定的话,这个方法不合适。这种写法相当于option含有text这个属性
$("#selectExample").find("option:contains('上海')").attr("selected",true);
$("#selectExample").find("option:contains('上海')").prop("selected",true);
通过判断是否包含某个值设置,如果有两个字符串开头的字母相同会造成不精确的问题 (比如’中国’/‘中国人’,通过‘中国’设置时,会默认选择最后一个包含’中国’的字符串)
多选框默认选中
$("#sel").val(Array('1','2'));//设置value=1和2的选项为默认选中
|