简体   繁体   English

禁用已在另一个多选框中选择的选项

[英]Disable options already selected in another multiple select box

I have seen this working with a single select box, but couldn't manage to do it with multiple select boxes. 我已经看到这个使用单个选择框,但无法设法使用多个选择框。

Let's say i have two multiple selects: 假设我有两个多选:

<select class='test' multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

<select class='test' multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

How can I manage to lock the option audi if selected in any of the select boxes? 如果在任何选择框中选择,我如何设置锁定选项audi?

I was trying using: 我正在尝试使用:

    $(document).on('change', function(e) {
         e.preventDefault();
         $('.test').find('option').prop('disabled', false);
         $('.test').each(function() {
         $('.test').not(this).find('option[value="' + this.value + '"]').prop('disabled', true);
         });
    });

You need to save the value and pass it to the other option. 您需要保存该值并将其传递给其他选项。 Like this: 像这样:

 $('.test').on('change', function() {
    var selected = $(this).children(':selected').val();
    $('.test').val(selected);
 });

http://codepen.io/sandrina-p/pen/KrZRXj http://codepen.io/sandrina-p/pen/KrZRXj

  • EDIT - i changed it to accept an array of values 编辑 - 我改变它接受一组值

     $('.test').on('change', function() { var selected = $(this).val(); $('.test').val(selected); }); 
  • EDIT - I recommend to use checkboxs than multiple select. 编辑 - 我建议使用复选框而不是多个选择。 Is more user friendly, intuitive and easier to select/disable the different options. 更加用户友好,直观,更容易选择/禁用不同的选项。 (see the codepen) (参见codepen)

     // 2 arguments: the first group of cars and the other group of cars function selectCar(carInputs, otherCarInputs) { carInputs.each(function(){ if ($(this).is(":checked")) { // it is checked var value = $(this).val(); console.log('checked: '+ value ); //filter otherCarsInputs and disable the one that the user selected. otherCarInputs.filter(function(){ if(this.value === value) { return true; } }).prop('disabled', 'disabled'); } else { var value = $(this).val(); console.log('unchecked: '+ value ); //do the inverse. is the user uncheck the input, enable it on the other team otherCarInputs.filter(function(){ if ($(this).is(':disabled') && this.value == value) { return true; } }).prop('disabled', ''); } }); } //on each input, calls the function with the right parameters (group car) $('input').on('change', function(){ if ($(this).hasClass('car1') ) { selectCar($('.car1'), $('.car2')); } else { selectCar($('.car2'), $('.car1')); } }); 

You can do it other way 你可以用其他方式做到

$('.test option').click(function(){
  var op = $(this).attr('value');
  // commented out for multiple select ability
  //$('.test option').attr('disabled', null);
  $('.test option[value='+op+']').not(':selected').attr('disabled', $(this).filter(':selected').length > 0 ? true : null);
});

I find this to be shorter and more straightforward using jQuery: 我发现使用jQuery更短更直接:

$('#select-element-id option').on('click', function() {
        $(this).attr('disabled', true);
});

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

相关问题 隐藏已在 select 框中选择的选项 ReactJS - Hiding options already selected in a select box ReactJS 如何禁用已经在另一个中选择的选项 <select> ? - how to disable an option already selected in another <select>? 在选择框中获取多个选定选项的列表 - Get List of Multiple Selected Options in Select Box 从选择框中删除选项(如果已在其他位置选择) - Remove options from select box if it is already selected in onother 根据另一个选择中选择的值禁用或启用选择选项 - Disable or Enable select Options based on value selected in another select javascript-从另一个选择中选择一个选项时,从多个选择框中隐藏选项 - javascript - Hide Options from Multiple Selection Box when an option from another select is selected 当选择另一个框时,从选择框中删除“禁用”属性 - Remove 'disable' property from select box when another box is selected 禁用 <select>在另一个选择时选项<select>框 - Disable <select> option when selected in another <select> box jQuery / Grails将多个选项/值标记为在多个选择框中选择 - JQuery/Grails marking multiple options/values as selected in multiple select box 如何使用Javascript Prototype从多个选择框中删除所选选项? - How to remove selected options from multiple select box with Javascript Prototype?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM