使用jQuery通过文本内容选择选项 [英] Selecting option by text content with jQuery

查看:77
本文介绍了使用jQuery通过文本内容选择选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个下拉框设置为使用jquery通过查询字符串传递的任何内容.

I want to set a dropdown box to whatever has been passed through a querystring using jquery.

如何将所选属性添加到选项中,以使"TEXT"值等于查询字符串中的某个参数?

How do I add the selected attribute to an option such that the "TEXT" value is equal to a certain param from the query string?

 $(document).ready(function() {
        var cat = $.jqURL.get('category');
        if (cat != null) {
            cat = $.URLDecode(cat);
            var $dd = $('#cbCategory');
            var $options = $('option', $dd);
            $options.each(function() {
                if ($(this).text() == cat)
                    $(this).select(); // This is where my problem is
            });
        };
    });

推荐答案

替换此:

var cat = $.jqURL.get('category');
var $dd = $('#cbCategory');
var $options = $('option', $dd);
$options.each(function() {
if ($(this).text() == cat)
    $(this).select(); // This is where my problem is
});

与此:

$('#cbCategory').val(cat);

在选择列表上调用 val() 会自动选择带有该选项的选项值(如果有).

Calling val() on a select list will automatically select the option with that value, if any.

这篇关于使用jQuery通过文本内容选择选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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