简体   繁体   English

如何在第二个选择框中禁用选择2个js选定值

[英]How to Disable select 2 js selected values on second select box

i got two select boxes both using select2 js, on the first one you can choose just one value, and on second you can choose multiple values, so what i want is to make this stuff work like this - if i choose "somecategory" on the second select box this option must be disabled, and if i choose "othercategory" on the select box the same option should be disabled on the first box as well. 我有两个都使用select2 js的选择框,第一个可以选择一个值,第二个可以选择多个值,所以我想要的是使这些东西像这样工作-如果我选择“ somecategory”第二个选择框必须禁用此选项,如果我在选择框上选择“ othercategory”,则第一个选择框也应禁用相同的选项。

here is the 这里是

    <select name="category" class="form-control catselect">
  /* here is some foreach stuff */
<option value="<?php echo $here we get ID;?>" <?php echo $here we get selected="selected";?>><?php echo $sub.lang_key($row->title);?></option>
</select>

<select name="multicat[]" class="form-control catmultiselect "  multiple="multiple">
  /* here is some foreach stuff */
<option value="<?php echo $here we get ID;?>" <?php echo $here we get selected="selected";?>><?php echo $sub.lang_key($row->title);?></option>
</select>

First, set your multicategory select box to be disabled. 首先,将您的多类别选择框设置为禁用。 Next, give your select boxes an ID (this is optional. You can reference the select boxes by name instead of ID using "[name='category']" ). 接下来,为您的选择框指定一个ID(这是可选的。您可以按名称而不是使用"[name='category']"来引用选择框)。 Then add an event handler to the select box: 然后将事件处理程序添加到选择框:

$("#categorySelect").on("select2:select", function(e){
    var value = $("#categorySelect").val();
    var multicat = $("#multiCategorySelect");
    if(value == "othercategory") {
        multicat.prop('disabled', false); // Enable the multi-category select box
    } else {
        multicat.val(''); // If you want to clear the selection
        multicat.prop('disabled', true); // Set it back to disabled
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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