在 jquery 1.3.2 中,以下内容有效:
<select id="c">
<option value="325">Red</option>
<option value="833">Purple</option>
</select>
$('#c').val('Red');
它将选择更改为以红色为标签的选项。在 jQuery 1.4 中这会失败。我怎样才能在1.4中得到相同的结果?这是 1.3 版本中的错误吗?
最佳答案
你必须这样做:
$('option:contains("Red")', '#c')[0].selected = true
编辑
@托马拉克
如果标签不互相排斥,您需要重写选择器:
$.fn.labselect = function(str) {
$('option', this).filter(function() {
return $(this).text() == str;
})[0].selected = true;
return this;
};
// Use it like this
$('#c').labselect('Red');
关于jquery - 在 1.4 中通过 jquery 设置按标签选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2324420/