在1.4中通过jquery设置按标签选择 [英] Setting a select by label via jquery in 1.4

查看:71
本文介绍了在1.4中通过jquery设置按标签选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jquery 1.3.2中,以下工作有效:

In jquery 1.3.2, the following works:

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

$('#c').val('Red');

然后将选择更改为带有红色标签的选项.在jQuery 1.4中,此操作失败.在1.4中如何获得相同的结果?这是1.3版中的错误吗?

And it changes the select to the option with RED as its label. In jQuery 1.4 this fails. How can I get the same result in 1.4? Was this a bug in the 1.3 version?

推荐答案

您必须这样做:

$('option:contains("Red")', '#c')[0].selected = true

编辑

@Tomalak

如果标签不互斥,则需要重写选择器:

If the labels arent mutually exclusivey you'd need to rewrite the selector:

$.fn.labselect = function(str) {
    $('option', this).filter(function() {
       return $(this).text() == str;
    })[0].selected = true;

    return this;
};

// Use it like this
$('#c').labselect('Red');

这篇关于在1.4中通过jquery设置按标签选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆