【问题标题】:Disable 2nd dropdown option based on first dropdown根据第一个下拉菜单禁用第二个下拉选项
【发布时间】:2015-07-24 11:26:49
【问题描述】:

我有两个动态下拉菜单,但下拉菜单的值和选项都相同。我想要的是,如果用户从第一个下拉列表中选择“苹果”,那么第二个下拉列表的苹果选项将被禁用(使用 javascript)。简而言之,用户不能从两者中选择相同的值。

//first drop down
<select name="fruit1">
    <option value="1">Apple</option>
    <option value="2">Mango</option>
</select>

//second dropdown  
<select name="fruit2">
    <option value="1">Apple</option>
    <option value="2">Mango</option>
</select>

我已经尝试过使用 jQuery:

function witness()
{
    var op=document.getElementById("witness1").value;
    $('option[value='+op+']').prop('disabled', true);
}

但是,这两个下拉菜单的值都被禁用,如果我选择芒果,那么苹果将不会启用它保持禁用状态。我知道我没有传递 id 所以两个下拉值都被禁用但我应该在哪里传递?

如果用户选择苹果,那么在第二个下拉菜单中苹果将被禁用,我想使用 Javascript 或 jQuery 来执行此操作。

【问题讨论】:

  • 到目前为止有什么尝试?
  • 查看我编辑的代码!
  • name 属性在您的代码中是双倍的。
  • 你应该试试 jqueys $('#YourSelect').change(function(){alert('first select changed');});
  • 我建议您不要通过抛出消息而不是禁用选项来让用户选择该选项。

标签: javascript jquery html


【解决方案1】:

小提琴:https://jsfiddle.net/3pfo1d1f/

要获得您所追求的功能,您需要在第一个下拉列表中挂钩 change 事件,以便在第二个下拉列表中禁用匹配元素。

我还将第二个下拉列表中的第一个元素初始化为禁用(在第一个下拉列表中默认选择此元素)

按原样使用 jquery:

HTML:

<!-- first dropdown -->
<select id="fruit1">
    <option value="1">Apple</option>
    <option value="2">Mango</option>
</select>

<br /> <br />

<!-- second dropdown -->
<select id="fruit2">
    <option value="1" disabled>Apple</option>
    <option value="2">Mango</option>
</select>

JQuery:

$('#fruit1').on( "change", function() {
    var op = $( this ).val();
    $('#fruit2 option').prop('disabled', false);
    $('#fruit2 option[value='+op+']').prop('disabled', true);
});

无论您在两个下拉菜单中有多少选项,这应该仍然有效

【讨论】:

  • 谢谢亲爱的...但是在第一次选择时它不起作用。之后它的工作完美。
  • 您的意思是,您希望它在第二个项目中自动选择一个非冲突项目,并且只是禁用该选项?它肯定可以做到这一点。 jsfiddle.net/3pfo1d1f/3
【解决方案2】:

试试这个:

HTML:

<select id='fruit1' onchange="witness();">
    <option selected></option>
    <option value="1">Apple</option>
    <option value="2">Mango</option>
</select>

<select id='fruit2'>
    <option selected></option>
    <option value="1">Apple</option>
    <option value="2">Mango</option>
</select>

JQuery:

function witness(){
    $("#fruit2 option").each(function(){
        if($("#fruit1 option:selected").val() == $(this).val())
            $(this).attr("disabled", "disabled");
        else
            $(this).removeAttr("disabled");
    });
}

您可以在此处查看一个工作示例: https://jsfiddle.net/mqjxL4n0/

【讨论】:

  • 请注意,此函数还会重新启用第二个选择元素中未在第一个选择元素上选择的选项。
【解决方案3】:
<select name="firstselect" id="firstselect">
   <option value="apple">Apple</option>
   <option value="orange">Orange</option>
</select>

<select name="secondselect" id="secondselect">
   <option value="apple">Apple</option>
   <option value="orange">Orange</option>
</select>

<script>
   $(document).ready(function(){
      $('#firstselect').change(function(){
         var firstselected = $(this).val();
         if(firstselected ){
             $('#secondselect option').each(function(){
                $(this).prop('disabled', false);
                if($(this).val()==firstselected )
                $(this).prop('disabled', true);
             });
         }
         else {
             $('#secondselect option').each(function(){
                $(this).prop('disabled', false);
             });
         }
      });
   });
</script>

【讨论】:

  • 也许您必须将 id 添加到您的选择中或将 idselection 更改为名称选择
猜你喜欢
  • 2019-02-14
  • 1970-01-01
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-29
  • 2015-12-01
相关资源
最近更新 更多