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

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
});

With this:

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

在 Select 列表上调用val()将自动 Select 具有该值的选项(如果有的话).

Jquery相关问答推荐

bootstrap - Select Select 器不能在下拉菜单中 Select

如何向父元素添加类?

在 JSON 对象上使用 jQuery 的 find()

JSLint 错误:将调用移动到包含函数的括号中

提醒未保存的表单更改

在范围内设置文本

jQuery.parseJSON 单引号与双引号

什么是不使用 jQuery 的经验技术原因?

我们如何在不重新加载页面的情况下使用 javascript/jQuery 更新 URL 或查询字符串?

为什么我们在 jQuery 中使用({ })?

错误找不到 jquery-2.0.2.min.map

如何在 Slick 轮播项目之间添加空格

jQuery 对象和 DOM 元素

当ID包含方括号时按ID查找DOM元素?

jQuery '如果 .change() 或 .keyup()'

带有 jquery 的 jsonp

请求的资源错误中不存在Access-Control-Allow-Origin标头

jQuery clone() 不克隆事件绑定,即使使用 on()

使用 TypeScript 设置 window.location

将变量的值复制到另一个