[英]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.