防止在三个不同的下拉列表中选择相同的值

时间:2013-06-11 10:20:10

标签: javascript html drop-down-menu

我有三个下拉菜单。我希望当用户从下拉列表1中选择任何项目时,应该在下拉列表2和3中禁用该项目。此外,如果在下拉列表2中选择了某个项目,则必须从下拉列表3中禁用所有选定项目。

以下是我正在使用的代码:

<!DOCTYPE html>
<html>
<head>
  <script>
    function updateSelect(changedSelect, selectId) {
      var otherSelect = document.getElementById(selectId);
      for (var i = 0; i < otherSelect.options.length; ++i) {
        otherSelect.options[i].disabled = false;
      }
      if (changedSelect.selectedIndex == 0) {
        return;
      }
      otherSelect.options[changedSelect.selectedIndex].disabled = true;
    }
  </script>
</head>
<body>
  <select id="select_1"  onchange="updateSelect(this,'select_2'),updateSelect(this,'select_3');"   name="indication_subject[]">
    <option value="" selected="selected">a </option>
    <option value="1"> Accounting</option>
    <option value="2"> Afrikaans</option>
    <option value="3"> Applied Information and Communication Technology</option>
    <option value="4"> Arabic</option>
    <option value="5"> Art and Design</option>
    <option value="6"> Biology</option>
    <option value="7"> Business Studies</option>
  </select>
  <select id="select_2" name="indication_subject[]" onchange="updateSelect(this,'select_1','select_3');" >
    <option value="" selected="selected">a </option>
    <option value="1"> Accounting</option>
    <option value="2"> Afrikaans</option>
    <option value="3"> Applied Information and Communication Technology</option>
    <option value="4"> Arabic</option>
    <option value="5"> Art and Design</option>
    <option value="6"> Biology</option>
    <option value="7"> Business Studies</option>
  </select>
  <select id="select_3" name="indication_subject[]" onchange="updateSelect(this,'select_1','select_2');" >
    <option value="" selected="selected">a </option>
   <option value="1"> Accounting</option>
   <option value="2"> Afrikaans</option>
   <option value="3"> Applied Information and Communication Technology</option>
   <option value="4"> Arabic</option>
   <option value="5"> Art and Design</option>
   <option value="6"> Biology</option>
   <option value="7"> Business Studies</option>
  </select>
</body>
</html>

下面是JSFiddle链接,它适用于两个下拉菜单,而不是三个。如何为此添加第三个下拉列表?

http://jsfiddle.net/x4E5Q/1/

3 个答案:

答案 0 :(得分:10)

试试这个:

<强> HTML

<select id="select1" name="indication_subject[]">
  <option value="" selected="selected">a </option>
  <option value="1"> Accounting</option>
  <option value="2"> Afrikaans</option>
  <option value="3"> Applied Information and Communication Technology</option>
  <option value="4"> Arabic</option>
  <option value="5"> Art and Design</option>
  <option value="6"> Biology</option>
  <option value="7"> Business Studies</option>
</select>

<select id="select2" name="indication_subject[]">
  <option value="" selected="selected">a </option>
  <option value="1"> Accounting</option>
  <option value="2"> Afrikaans</option>
  <option value="3"> Applied Information and Communication Technology</option>
  <option value="4"> Arabic</option>
  <option value="5"> Art and Design</option>
  <option value="6"> Biology</option>
  <option value="7"> Business Studies</option>
</select>

<select id="select3" name="indication_subject[]">
  <option value="" selected="selected">a </option>
  <option value="1"> Accounting</option>
  <option value="2"> Afrikaans</option>
  <option value="3"> Applied Information and Communication Technology</option>
  <option value="4"> Arabic</option>
  <option value="5"> Art and Design</option>
  <option value="6"> Biology</option>
  <option value="7"> Business Studies</option>
</select>

<强> JS

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

Demo

答案 1 :(得分:0)

See below code

    $(document).ready(function() {
      $("select").on('hover', function () {
            $previousValue = $(this).val();
        }).change(function() {
            $("select").not(this).find("option[value='"+ $(this).val() + "']").attr('disabled', true);
            $("select").not(this).find("option[value='"+ $previousValue + "']").attr('disabled', false);
        });
    });

$("#confirm-form").submit(function(e) {
    e.preventDefault();
    $("select").find("option").removeAttr('disabled');
    document.getElementById('confirm-form').submit();
});

答案 2 :(得分:0)

$(document).ready(function(){  

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

        $("select").not(this).find("option[value='"+ $(this).val() + "']").attr('disabled', true);

    });


});