简体   繁体   中英

Manually selecting a multiple drop down option if value is matched in JQuery

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

选项具有selected属性:

this.selected = true;
$(this).attr('selected', true);

应该做魔术。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM