使用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.