开发者

If option is selected in one multiselect, set it to disabled in another dropdown

开发者 https://www.devze.com 2023-02-17 05:05 出处:网络
What is wrong with this code?I have a multiselect (VendorIDs) and a select (vendorDropDown).I want to set a开发者_Python百科ll the selected items in VendorIDs to disabled in vendorDropDown.This doesn\

What is wrong with this code? I have a multiselect (VendorIDs) and a select (vendorDropDown). I want to set a开发者_Python百科ll the selected items in VendorIDs to disabled in vendorDropDown. This doesn't work:

            $('#Vendor_IDs :selected').each(function (i, selected) {
                var v = $(selected).val();
                $("#vendorDropDown option[value='" + v + "']").attr("disabled", true);
            });

But, this does:

            $('#Vendor_IDs :selected').each(function (i, selected) {
                var v = $(selected).val();
                alert(v);
                $("#vendorDropDown option[value='" + v + "']").attr("disabled", true);
            });

How does adding the alert change anything?


It looks like var v = $(selected).val(); takes too long to execute for the next line to be on time. Try this:

 $('#Vendor_IDs :selected').each(function (i, selected) {
     $("#vendorDropDown option[value='" + $(selected).val() + "']").attr("disabled", true);
 });

This way, the second line is not dependent on the first line that was taking too long to execute. Let me know if it works.

-edited, 2nd try!-

 $('#Vendor_IDs :selected').each(function() {
     $("#vendorDropDown option[value='" + $(this).val() + "']").attr("disabled", true);
 });
0

精彩评论

暂无评论...
验证码 换一张
取 消