简体   繁体   English

更改一个选择选项会更改许多其他选项(JavaScript,JQuery)

[英]Changing one select option changes many others (JavaScript, JQuery)

I have a lot of select drop downs on a jsp page with a long list of elements. 我在jsp页面上有很多选择下拉列表,其中包含很长的元素列表。 All of these drop downs have the same list of elements. 所有这些下拉列表都具有相同的元素列表。 Say I have to get the choice in descending order of preference from the user. 假设我必须从用户那里按优先级的降序进行选择。 I made (many) selects in the following way: 我用以下方式做了(很多)选择:

<select id="sel1" class="myClass">
        <script>
         populate(document.getElementById('sel1'));
        </script>
  </select>

... ...

<script>
function populate(op1)
{

    var myArray = ["Chinese", "Italian", "Indian", ...//a long list of elements
        var sel = op1;
        for(var i = 0; i < myArray.length; i++) {
            var opt = document.createElement('option');
            opt.innerHTML = myArray[i];
            opt.value = myArray[i];
            sel.appendChild(opt);
        }
}
</script>

I have to create javascript/JQuery code in such a way that if a user selects an option the first select, that option gets disabled/removed in the others, leaving room for changes later. 我必须以这样的方式创建javascript / JQuery代码:如果用户选择第一个选项的选项,则该选项在其他选项中被禁用/删除,从而为以后的更改留出空间。 Say, the user's preference order is: Chinese, Indian, Italian... then on selecting Chinese in the first drop down, it gets disabled/removed from the other drop downs. 比如说,用户的偏好顺序是:中文,印度,意大利......然后在第一个下拉菜单中选择中文,它会被禁用/从其他下拉菜单中移除。 Then, on selecting Indian from the second, it gets disabled/removed from all the others (including the previous one). 然后,从第二个选择印第安人时,它将被禁用/从所有其他人(包括前一个)中删除。 Now, if the user decides his order of preference is actually Chinese, Italian, Indian, .. he should be able to change his choice in such a way that the code doesn't break down. 现在,如果用户决定他的偏好顺序实际上是中文,意大利语,印度语,那么他应该能够改变他的选择,使代码不会崩溃。 Say, we can have a button for reset and it resets all the choices by calling this function: 比如,我们可以有一个重置按钮,它通过调用此函数重置所有选项:

function resetFunc()
{
    var options = document.getElementsByClassName("myClass");
        for (var i = 0, l = options.length; i < l; i++) 
        {
            options[i].selectedIndex = "0";
        }
}

Any idea how to accomplish this? 知道怎么做到这一点? I need the code to be browser independent (while googling, I read somewhere that IE doesn't support removal of elements from drop down). 我需要代码与浏览器无关(当谷歌搜索时,我在某处读到IE不支持从下拉列表中删除元素)。

EDIT: Here's what I basically want: 编辑:这是我基本上想要的:

http://jsfiddle.net/RaBuQ/1/ http://jsfiddle.net/RaBuQ/1/

However, there's a problem in this. 但是,这有一个问题。 If a user keeps changing his choices, this thing breaks down. 如果用户不断改变他的选择,这件事就会崩溃。 I'm able to select multiple choices. 我可以选择多个选项。

$('select').change(function(){
    var v = $(this).val();
    $('select option[value="'+$(this).data('old-val')+'"]').prop('disabled', false);
    $(this).data('old-val',v);
    if(v != "0"){
        $('select option[value="'+v+'"]').not(this).prop('disabled',true);
    }
});

Here's a fiddle. 这是一个小提琴。

If I selected 'Football', 'Golf', 'Tennis', I'd need to select 'No preference' in the third box before I could then select it in one of the other boxes. 如果我选择了“足球”,“高尔夫”,“网球”,我需要在第三个框中选择“无偏好”,然后才能在其他一个框中选择它。 I think this is acceptable from a UX perspective. 从UX的角度来看,我认为这是可以接受的。

Since you've tagged this jQuery my example below will utilize that: 既然你已经标记了这个jQuery,我的下面的例子就会利用它:

 function populate() { var myArray = ["Chinese", "Italian", "Indian"]; $('.myClass').each(function() { var dis = $(this); dis.append($("<option>").attr("value", "").text("select")); $.each(myArray, function(i, o) { dis.append($("<option>").attr("value", o).text(o)); }); }); } function init() { $('.myClass').html('').prop('disabled', false); populate(); } $(document).on('change', '.myClass', function() { $('.myClass option[value="' + $(this).val() + '"]:not(:checked)').remove(); $(this).prop('disabled', true); }); $('#reset').click(init); init(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <select id="sel1" class="myClass"></select> <select id="sel2" class="myClass"></select> <select id="sel3" class="myClass"></select> <input type="button" id="reset" value="Reset options" /> 

The following might not be the most efficient solution, but you should try it if there is nothing better: when you change a select, empty all other selects and then fill them with all the other options. 以下可能不是最有效的解决方案,但如果没有更好的方法,您应该尝试它:当您更改选择时,清空所有其他选择,然后用所有其他选项填充它们。

lets say you have 3 selects: sel1, sel2, sel3 in the onchange event, you could call a function "fill_other_sel(number)" where number is the number of the current selector. 假设您在onchange事件中有3个选择:sel1,sel2,sel3,您可以调用函数“fill_other_sel(number)”,其中number是当前选择器的编号。

This function should delete current options and then populate checking with the previous selectors so that you dont populate with a previously selected value. 此函数应删除当前选项,然后使用先前的选择器填充检查,以便不填充先前选择的值。

function fill_other_sel(number){
   var num_selectors = 3;
   while (number <= num_selectors){
      number++;
      $('#sel'+number).options.length=1;
      populate('sel'+number, already_selected_values_array);
   }

}

also you might add a parameter to your populate function showing which values have already been selected to prevent them from appearing again 您也可以在填充函数中添加一个参数,显示已经选择了哪些值以防止它们再次出现

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

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