我想将下拉框设置为使用 jquery 通过查询字符串传递的任何内容。
如何将所选属性添加到选项,使“TEXT”值等于查询字符串中的某个参数?
$(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()
在选择列表上将自动选择具有该值的选项(如果有)。
关于jquery - 使用 jQuery 按文本内容选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1009740/