使用 jQuery 预选多选中的值

发布于 2024-08-20 07:52:43 字数 415 浏览 5 评论 0原文

我有一组出现在页面加载中的值: 例如[1193,1184,2372]。

我有一个下拉清单,它可以像这样采用单个值 $(".ddl").dropdownchecklist().val(1193);

当我重新绑定下拉列表时,它会正确选择值。但是,我尝试将包含多个值的数组传递给 val() 方法,但没有选择这些值。我有点期待这个。我不确定如何最好地进行预选。

我尝试迭代我的数组来构建要应用的过滤器属性,如下所示: $(".ddl [value*='1184'][value*='9067'][value*='14841']").attr('已选择','已选择');

但这行不通。我不想迭代 ddl 中的所有选项,并在匹配时将它们设置为已选择,因为有数百个选项,这不会带来良好的性能,特别是如果只有 2 个选项需要预先选择。

有什么想法吗?

I have a set of values that are present on the page loading:
eg [1193,1184,2372].

I have a dropdownchecklist which can take a single value like so
$(".ddl").dropdownchecklist().val(1193);

When I rebind the dropdownchecklist this selects the value correctly. However I've tried passing my array containing multiple values to the val() method and that doesn't select the values. I kind of expected this. I'm not sure how to best go about preselecting.

I've tried iterating my array to build an filter attribute to apply as the following:
$(".ddl [value*='1184'][value*='9067'][value*='14841']").attr('selected','selected');

but this doesn't work. I don't want to have to iterate all the options in the ddl and set them to selected if they match as there are hundreds of options and this won't be good performance wise, especially if there are only 2 options to be preseleted.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何以笙箫默 2024-08-27 07:52:43

创建所选字符串的字符串以创建 jQuery 选择器字符串并将其放入 .find() 函数中:

var str = '';
var arrLength = selectedArray.length;
$.each(selectedArray, function(i){
   str += 'option[value*='+selectedArray[i]+']';
   if((i+1) != arrLength){
     str += ', ';
   }
});

$('.ddl').find("option[value*='1184'], option[value*='9067'], option[value*='14841']")
     .attr('selected','selected'); 

另请注意,在将下拉列表与值进行比较时,您正在查找选项 select 中的元素,而不是 select 元素本身。

Create a string of the selected ones to create the jQuery selector string and put it into the .find() function:

var str = '';
var arrLength = selectedArray.length;
$.each(selectedArray, function(i){
   str += 'option[value*='+selectedArray[i]+']';
   if((i+1) != arrLength){
     str += ', ';
   }
});

$('.ddl').find("option[value*='1184'], option[value*='9067'], option[value*='14841']")
     .attr('selected','selected'); 

Also note, when comparing a drop down with a value, you're looking for the option element in the select, not the select element itself.

眼波传意 2024-08-27 07:52:43

我得到了它,但上面的答案看起来像是一个很好的解决方案:

$.each(arr, function(i,s)
{
     $(ddl_id + ' [value*=\'' + s + '\']').attr('selected','selected');
});    

不确定哪个可能更快,但矿井更干净。

I got it working with this but the above answer looks like a good solution:

$.each(arr, function(i,s)
{
     $(ddl_id + ' [value*=\'' + s + '\']').attr('selected','selected');
});    

Not sure which is likely to be faster but mines a little cleaner.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文