在1.4中通过jquery设置选择标签

Sto*_*ped 6 jquery

在jquery 1.3.2中,以下工作:

<select id="c">
  <option value="325">Red</option>
  <option value="833">Purple</option>
</select>

$('#c').val('Red');
Run Code Online (Sandbox Code Playgroud)

并且它将选项更改为选项,并将RED作为其标签.在jQuery 1.4中,这失败了.如何在1.4中获得相同的结果?这是1.3版本中的错误吗?

aef*_*fxx 12

你必须这样做:

$('option:contains("Red")', '#c')[0].selected = true
Run Code Online (Sandbox Code Playgroud)

编辑

@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');
Run Code Online (Sandbox Code Playgroud)