简体   繁体   English

互斥选择/下拉菜单

[英]Mutually exclusive selects/dropdowns

I am trying to create mutually exclusive dropdowns using JS. 我正在尝试使用JS创建互斥的下拉列表。

Only one OS can be selected from these 4: image 这4个映像只能选择一个操作系统:

When one is selected, the others should be disabled. 选择一个后,其他的应禁用。

HTML part: HTML部分:

<table id="table_os" class="table table-bordered">
<tbody>
<tr>
    <td class="text-center">
        <div><img src="distro-redhat.png"></div>
        <div><h6><span class="semi-bold">Red Hat Linux</span></h6></div>
        <select class="form-control" style="text-align-last: center;">
            <option value="" selected disabled>Select version</option>
            <option value="rhel7">7 (latest) </option>
        </select>
    </td>
    <td class="text-center">
        <div><img src="oel.png"></div>
        <div><h6><span class="semi-bold">Oracle Linux</span></h6></div>
        <select class="form-control" style="text-align-last: center;">
            <option value="" selected disabled>Select version</option>
            <option value="oel7">7 (latest)</option>
        </select>
    </td>
    <td class="text-center">
        <div><img src="centos.png"></div>
        <div><h6><span class="semi-bold">CentOS Linux</span></h6></div>
        <select class="form-control" style="text-align-last: center;">
            <option value="" selected disabled>Select version</option>
            <option value="centos7">7 (latest)</option>
        </select>
    </td>
    <td class="text-center">
        <div><img src="windows.png"></div>
        <div><h6><span class="semi-bold">Microsoft Windows</span></h6></div>
        <select class="form-control" style="text-align-last: center;">
            <option value="" selected disabled>Select version</option>
            <option value="win2012r2">Standard 2012 R2</option>
        </select>
    </td>
</tr>
</tbody>

Asking for help for the JS part. 向JS部分寻求帮助。

UPDATE: Perhaps, I misled everyone by saying "others should be disabled". 更新:也许,我通过说“其他人应该被禁用”误导了所有人。 Naturally, it should be possible to select any of the values but in such a way that only one is selected at all times. 自然,应该可以选择任何一个值,但是必须始终选择一个。 For instance: currently, I select Red Hat > 7 and the others get disabled. 例如:当前,我选择Red Hat> 7,其他的将被禁用。 But now if I want to select Oracle Linux > 7, I should be allowed to do so. 但是现在,如果我想选择Oracle Linux> 7,则应该允许这样做。 Meaning that the other dropdowns should "reset" to "Select version" 意味着其他下拉菜单应“重置”为“选择版本”

This can be done by listening to the change event of the <select> elements then utilizing jQuery not() to target other <select> elements: 这可以通过侦听<select>元素的change事件,然后利用jQuery not()定位其他<select>元素来完成:

$(document).ready(function() {
  var $table = $('#table_os');

  $table.find('select').on('change', function() {
    var val = $(this).val();

    if (val !== '') {
      $(this).closest('tr').find('select').not($(this)).prop('disabled', true);
    }
  });
});

Here is a jsfiddle demonstrating the functionality. 这是一个演示功能的jsfiddle

As the first <option> of each select is disabled due to HTML disabled property you have in your provided markup, once they have been disabled they will remain that way. 由于每个选择的第一个<option>由于您在提供的标记中具有HTML disabled属性而被禁用,因此一旦禁用它们,它们将保持这种状态。

If you remove the disabled property on the default/first <option> of each <select> you can easily build functionality to re-enable other dropdowns if the user selects this default/first option in case they change their mind. 如果您在每个<select>的默认/第一个<option>上删除了disabled属性,则如果用户选择此默认/第一个选项以防万一,他们可以轻松地构建功能来重新启用其他下拉菜单。 Here is the jsfiddle : 这是jsfiddle

$(document).ready(function(){
  var $table = $('#table_os')

  $table.find('select').on('change', function(){
    var val = $(this).val();

    var $otherSelects = $(this).closest('tr').find('select').not($(this));

    if(val !== '') {
        $otherSelects.prop('disabled', true);
    }
    else {
        $otherSelects.prop('disabled', false);
    }
  });
});

Hopefully this helps! 希望这会有所帮助!

Add function to each select box and onchange call function to disable rest of select elements 向每个选择框添加功能,并通过onchange调用功能禁用其余选择元素

 function selectThis(obj){ var selectelems=document.getElementsByTagName('select') for(var i=0;i<selectelems.length;i++){ if(selectelems[i]!=obj){ selectelems[i].setAttribute('disabled','true') } } } 
 <table id="table_os" class="table table-bordered"> <tbody> <tr> <td class="text-center"> <div><img src="https://www.haskell.org/platform/img/distro-redhat.svg" width="60px"></div> <div><h6><span class="semi-bold">Red Hat Linux</span></h6></div> <select class="form-control" style="text-align-last: center;" onchange="selectThis(this)"> <option value="" selected >Select version</option> <option value="rhel7">7 (latest) </option> </select> </td> <td class="text-center"> <div><img src="http://austinlinux.com/local/Oracle%20Linux.jpg" width="51px"></div> <div><h6><span class="semi-bold">Oracle Linux</span></h6></div> <select class="form-control" style="text-align-last: center;" onchange="selectThis(this)"> <option value="" selected >Select version</option> <option value="oel7">7 (latest)</option> </select> </td> <td class="text-center"> <div><img src="http://seeklogo.com/images/C/centos-logo-494F57D973-seeklogo.com.png" width="60px"></div> <div><h6><span class="semi-bold">CentOS Linux</span></h6></div> <select class="form-control" style="text-align-last: center;" onchange="selectThis(this)"> <option value="" selected >Select version</option> <option value="centos7">7 (latest)</option> </select> </td> <td class="text-center"> <div><img src="https://images.seeklogo.net/2012/12/windows-8-icon-logo-vector-400x400.png" width="60px"></div> <div><h6><span class="semi-bold">Microsoft Windows</span></h6></div> <select class="form-control" style="text-align-last: center;" onchange="selectThis(this)"> <option value="" selected >Select version</option> <option value="win2012r2">Standard 2012 R2</option> </select> </td> </tr> </tbody> 

use this 用这个

$('select').on('change',function(){
     $(this).parents('tr').siblings('tr').find('select').not($(this)).prop('disabled', true);
});

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

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