tc.find("#autlist option['text='hello']").attr('selected',true);
我想设置text为hello的为选中项,是我没写对么?
确定autlist存在。谢谢各位帮忙
text不是option属性(attribute),是property,不能通过属性(attribute)[]选择器,用:contains,不过contains不能精确匹配,需要+filter查找,否则contains会把只要包含人hello内容 option的都会出来,如 hello a,hello b此类的option
tc.find("#autlist option:contains('hello')").filter(function(){return this.text=='hello'}).attr("selected",true);
Text 少个单引号!此外结束以后,看看option 元素能否选择到?
tc.find("#autlist option[value='hello']").attr('selected',true);
或者
tc.find("#autlist option:contains('hello')").attr("selected", true);
$("#currentPage").val(currentPage);
tc.find("#autlist option").each(function(){
$(this).text() == "hello" && $(this).attr('selected',true);
})
tc.find("#autlist option[value='hello']").prop('selected',true);
tc.find("#autlist option[value='hello']").attr('selected',true);