在 jquery 1.3.2 中,以下工作有效:
<select id="c">
<option value="325">Red</option>
<option value="833">Purple</option>
</select>
$('#c').val('Red');
它将选择更改为标签为 RED 的选项。在 jQuery 1.4 中这会失败。我怎样才能在1.4中得到相同的结果?这是1.3版本的bug吗?
你必须这样做:
$('option:contains("Red")', '#c')[0].selected = true
EDIT
@Tomalak
如果标签不是互斥的,您需要重写选择器:
$.fn.labselect = function(str) {
$('option', this).filter(function() {
return $(this).text() == str;
})[0].selected = true;
return this;
};
// Use it like this
$('#c').labselect('Red');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)