简体   繁体   English

具有相同选项的多个选择框-需要唯一选择

[英]multiple select boxes with same options - require unique selection

I have a form with 3 select boxes. 我有一个带有3个选择框的表格。 Each select box has the same options. 每个选择框都有相同的选项。 I want to require that you have to select a different option for each select box. 我想要求您必须为每个选择框选择一个不同的选项。 For example, let's say the options are "cat", "dog" and "bird". 例如,假设选项为“猫”,“狗”和“鸟”。 If someone selects "bird" in the first select box, I want to disable or hide that option from the other two boxes. 如果有人在第一个选择框中选择了“鸟”,我想在其他两个框中禁用或隐藏该选项。 If they change their selection, then "bird" will be enabled or unhidden again. 如果他们更改选择,那么“ bird”将被启用或再次隐藏。

<select id="box1">
    <option value="cat">Cat</option>
    <option value="dog">Dog</option>
    <option value="bird">Bird</option>
</select>

<select id="box2">
    <option value="cat">Cat</option>
    <option value="dog">Dog</option>
    <option value="bird">Bird</option>
</select>

<select id="box3">
    <option value="cat">Cat</option>
    <option value="dog">Dog</option>
    <option value="bird">Bird</option>
</select>

I assume I can do this with jquery onchange but I'm not sure how to require the unique selection across different select boxes. 我假设我可以使用jquery onchange来做到这一点,但是我不确定如何在不同的选择框中要求唯一选择。

$('select').on('change', function() {
    // If option is selected, disable it in all other select boxes. If option is deselected, reenable it in all other select boxes.
})

Thanks for your help! 谢谢你的帮助!

1) Top To Bottom Priority Approach 1)自上而下的优先方法

The flow must be top to bottom . 流程必须自上而下 This also means when ever the user changes the dropdown value all the next dropdown's which come after it must be reset. 这也意味着,无论何时用户更改下拉值,都必须重置下拉菜单中的所有下一个下拉菜单。 Having said this, here is my code snippet. 话虽如此,这是我的代码段。

 HandleDropdowns($('#box1')); //initially call this function to handle the dropdowns by passing the first dropdown as parameter $('select').on('change', function() { HandleDropdowns($(this)); // handle all dropdowns on any change event. }); function HandleDropdowns(element) { var $element = element; var value = $element.val(); $element.nextAll().val(''); //using nextAll lets reset all the following dropdowns $element.nextAll().attr('disabled', 'disabled'); //disable all the following dropdowns. HandleOptions(); // call this function to toggle the options if (value.length) { $element.next().removeAttr('disabled'); // only if this dropdown has some selection enable the next dropdown. } } function HandleOptions() { $('option').removeAttr('disabled'); //reset all the options to be available $.each($('select'), function() { //loop from top to bottom and disable the options one by one. var value = $(this).val(); if (value.length) { $(this).nextAll().find('option[value="' + value + '"]').attr('disabled', 'disabled'); } }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="box1"> <option value="">Select</option> <option value="cat">Cat</option> <option value="dog">Dog</option> <option value="bird">Bird</option> </select> <select id="box2"> <option value="">Select</option> <option value="cat">Cat</option> <option value="dog">Dog</option> <option value="bird">Bird</option> </select> <select id="box3"> <option value="">Select</option> <option value="cat">Cat</option> <option value="dog">Dog</option> <option value="bird">Bird</option> </select> 


2) All Select Box With Same Priority Approach 2)所有具有相同优先级方法的选择框

In this approach when ever the user selects a value we check if any other dropdown has the same value, If yes reset it else do nothing. 用这种方法,只要用户选择了一个值,我们就会检查其他下拉菜单是否具有相同的值,如果是,则将其重置,否则不执行任何操作。 Below is a working sample. 以下是一个工作示例。

 $('select').on('change', function() { HandleDropdowns($(this)); }); function HandleDropdowns(element) { var $element = element; var value = $element.val(); $.each($('select').not($element), function() { //loop all remaining select elements var subValue = $(this).val(); if (subValue === value) { // if value is same reset $(this).val(''); console.log('resetting ' + $(this).attr('id')); // demo purpose } }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="box1"> <option value="">Select</option> <option value="cat">Cat</option> <option value="dog">Dog</option> <option value="bird">Bird</option> </select> <select id="box2"> <option value="">Select</option> <option value="cat">Cat</option> <option value="dog">Dog</option> <option value="bird">Bird</option> </select> <select id="box3"> <option value="">Select</option> <option value="cat">Cat</option> <option value="dog">Dog</option> <option value="bird">Bird</option> </select> 

Try this 尝试这个

$('select').on('change', function () {
  $("select").find("option").removeAttr("disabled");
  $("select").not(this).find("option[value=" + $(this).val() + "]").attr("disabled", "disabled");
});

You can use onchange listener and evaluate the results simultaneously. 您可以使用onchange侦听器并同时评估结果。 Also add an reset button cause once three get selected, it becomes bottle neck case. 还要添加一个重置按钮,因为一旦选择了三个,它就会变成瓶颈。

The working code is given below 工作代码如下

 function changeSelect(elements) { var values = {}; elements.forEach(function(item){ values[item.id] = item.value; }); elements.forEach(function(item){ for(var i = 0; i < item.children.length; i++) { for(ids in values) { if(item.id != ids && item.children[i].value == values[ids]) { item.children[i].style.display = 'none'; } } } }); } function resetSelection(elements) { elements.forEach(function(item){ item.value = ''; for(var i = 0; i < item.children.length; i++) { item.children[i].style.display = ''; } }); } var box1 = document.getElementById('box1'); var box2 = document.getElementById('box2'); var box3 = document.getElementById('box3'); var boxArray = [box1, box2, box3]; boxArray.forEach(function(item){ item.addEventListener('change', changeSelect.bind(undefined,boxArray)); }); document.getElementById('reset').addEventListener('click', resetSelection.bind(undefined,boxArray)); 
 <select id="box1"> <option value="">Select An Option</option> <option value="cat">Cat</option> <option value="dog">Dog</option> <option value="bird">Bird</option> </select> <select id="box2"> <option value="">Select An Option</option> <option value="cat">Cat</option> <option value="dog">Dog</option> <option value="bird">Bird</option> </select> <select id="box3"> <option value="">Select An Option</option> <option value="cat">Cat</option> <option value="dog">Dog</option> <option value="bird">Bird</option> </select> <button id="reset">Reset</button> 

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

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