如果 JQuery 中的值匹配,则手动选择多个下拉选项
如果值匹配,我需要手动选择下拉选项。这是代码:
if($("#hiddenMyField").val() != "") {
$("#dropdown").each(function(i){
$('option', this).each(function() {
if($(this).html() == $("#hiddenMyField").val()) {
// code to select the option
} else {
alert('not matched');
}
});
});
}
如果满足条件,如何选择下拉列表中的当前选项?
谢谢
I need to manually select a dropdown option if a value is matched. Here is the code:
if($("#hiddenMyField").val() != "") {
$("#dropdown").each(function(i){
$('option', this).each(function() {
if($(this).html() == $("#hiddenMyField").val()) {
// code to select the option
} else {
alert('not matched');
}
});
});
}
How can I select the current option located in the drop down if that if condition is met?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
选项有一个
selected
属性:Options have a
selected
property:应该发挥魔法。
should do the magic.
好吧,我设法找到了解决方法。
由于选项通过 ui.dropdownchecklist.js 被转换为带有复选框的 div,我首先使用其正常视图加载多个下拉列表,然后使用 this.selected = true 选择必要的项目,然后加载 ui.dropdownchecklist.js函数,以便项目将被转换回带有复选框的 div。用户甚至看不到实际的多个复选框,所以这对我来说效果很好。当它们转换为复选框时,所选项目将被保留,并且在传输时也会被勾选。
All right I managed to find a workaround for this.
Since the options were being translated to divs with checkboxes with the ui.dropdownchecklist.js I first loaded the multiple dropdown with its normal view, then selected the necessary items with the this.selected = true and then I loaded the ui.dropdownchecklist.js function so the items will be translated back to divs with checkboxes. The user doesn't even see the actual multiple checkboxes so this worked out well for me. When they are translated to checkboxes, the selected items are kept and are also ticked when they are transferred.