使用 jQuery 预选多选中的值
我有一组出现在页面加载中的值: 例如[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建所选字符串的字符串以创建 jQuery 选择器字符串并将其放入 .find() 函数中:
另请注意,在将下拉列表与值进行比较时,您正在查找
选项
select 中的元素,而不是 select 元素本身。Create a string of the selected ones to create the jQuery selector string and put it into the .find() function:
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.我得到了它,但上面的答案看起来像是一个很好的解决方案:
不确定哪个可能更快,但矿井更干净。
I got it working with this but the above answer looks like a good solution:
Not sure which is likely to be faster but mines a little cleaner.