【问题标题】:Show/hide options from dropdown using jQuery使用 jQuery 从下拉菜单中显示/隐藏选项
【发布时间】:2014-10-01 13:17:19
【问题描述】:

我有 3 个下拉菜单,其中包含超过 4 个问题作为每个下拉菜单中的选项。我想要实现的是,当用户从任何下拉列表中选择一个选项时,该特定选项/问题必须从其他 2 个下拉列表中隐藏,并且当他更改他的选择时,该选项/问题必须再次显示在其他 2 个下拉列表中。 他可以从任何下拉列表中选择问题。这是我到目前为止所尝试的。这段特定的代码将隐藏 select 上的选项,但我不知道如何准确地显示它。

Javascript

var removeSelection = function (select) {
$('select').filter(':not(#' + select.attr('id') + ')').each(function () {
    var index = select.find(':selected').index();
    $(this).find('option:eq(' + index + ')').hide();
});
};

$(function () {
    $('select').change(function () {
        removeSelection($(this));
    });
});

HTML

<form id="form1">
 <select id="select1">
     <option id="selectOpt1">Question 1</option>
     <option id="selectOpt2">Question 2</option>
     <option id="selectOpt3">Question 3</option>
     <option id="selectOpt4">Question 4</option>
 </select>

    <select id="select2">
        <option id="selectOpt1">Question 1</option>
        <option id="selectOpt2">Question 2</option>
        <option id="selectOpt3">Question 3</option>
        <option id="selectOpt4">Question 4</option>
    </select>

    <select id="select3">
        <option id="selectOpt1">Question 1</option>
        <option id="selectOpt2">Question 2</option>
        <option id="selectOpt3">Question 3</option>
        <option id="selectOpt4">Question 4</option>
    </select>



</form>

JSFIDDLE- CLick Here

更新小提琴 Updated

场景 1 - 从任何下拉列表中选择一个选项。应该从其他下拉列表中禁用它。 场景 2 - 从同一个下拉列表中更改选项。应在其他下拉菜单中启用上一个选项。

【问题讨论】:

  • 你可以用来禁用它们而不是隐藏
  • @Kaushik 您无法隐藏选择 > 选项。你只能禁用它。
  • id 应该是唯一的。请改用class
  • @ShahRukh.. 他们中的任何一个都可以.. 我没有禁用问题。 I am stuck up with enabling the same when option has been changed from parent dropdown.
  • 我可以在您演示的所有 3 个下拉菜单中选择相同的选项。你检查它是否工作?

标签: javascript jquery html drop-down-menu


【解决方案1】:

将重复的id 更改为普通类后,您可以尝试这样的操作

$('select').change(function () {
    $("option:disabled").prop("disabled",false); // reset the previously disabled options
    var $selectedQ = $(this).find("option:selected"); // selected option
    var commonClass= $selectedQ.attr("class"); // common class shared by the matching options
    $("."+commonClass).not($selectedQ).prop("disabled","disabled"); // disable the matching options other than the selected one
});

Updated Fiddle

(如果选项有多个不同的类,这将不起作用,我会使用通用值或数据属性,而不是像)

$('select').change(function () {
  $("option:disabled").prop("disabled", false);
  var $selectedQ = $(this).find("option:selected")
  var value = $selectedQ.val();
  $("option[value='" + value + "']").not($selectedQ).prop("disabled", "disabled");
});

Demo

更新根据 cmets

$('select').change(function () {
  var prevMatches = $(this).data("prevMatches");
  if (prevMatches) prevMatches.prop("disabled", false)
     var $selectedQ = $(this).find("option:selected")
  var value = $selectedQ.val();
  var $matches = $("option[value='" + value + "']").not($selectedQ);
  $matches.prop("disabled", "disabled");
  $(this).data("prevMatches", $matches);
});

Demo

【讨论】:

  • Hey Tilwin .. 这会在我从其他下拉列表中选择另一个选项后启用第一个选择的选项...
  • @Kaushik 我读到你对另一个答案的评论,你希望它像this
  • 正确率 75%。它仍然不会在某个时间点启用其他选项。让我告诉你怎么做!从第一个下拉列表中选择问题 3。从第二个下拉列表中选择问题 4。从第三个下拉列表中选择问题 2。现在所有下拉菜单都将启用问题 1 选项。如果我在 Thrid 下拉列表中选择问题 1。问题 2 应该为所有下拉菜单启用。这就是我想要的。
  • Bingo.. 即使这正是我想要的。非常感谢您的回答.. :)
【解决方案2】:

你可以这样做:

var removeSelection = function (select) {
var id=select.attr("id");
$(".hide-"+id).show();
$('select').filter(':not(#' + select.attr('id') + ')').each(function () {
    var index = select.find(':selected').index();

    $(this).find("option").removeClass("hide-"+id);
    $(this).find('option:eq(' + index + ')').each(function(){
    if($(this).attr("id")!="selectOpt1"){
        $(this).addClass("hide-"+id);
    }
});
});
$(".hide-"+id).hide();

};

$(function () {
    $('select').change(function () {
    removeSelection($(this));
    });
});

JSFiddle

【讨论】:

  • 嗯,这部分满足了我的要求。首先,如果我从第一个下拉列表中选择问题 3,它将从其他 2 中删除。然后,如果我从第二个下拉列表中选择问题 2,从第三个下拉列表中选择问题 4,则问题 1 必须始终存在于所有下拉列表中。当我在第一个下拉列表中再次选择问题 1 时,首先选择的选项问题 3,将不会再次显示在其他 2 个下拉列表中...
  • 好的,我没有从问题中得到答案,现在它已更新为始终显示选项 1。
  • 没有没有。你理解错了。让我更新我的小提琴,让你解释一下场景。问题 1 不是默认问题。可以说我有第一个选项-选择问题-。每个下拉列表中有 4 个问题。场景 1 - 在任何下拉列表中选择的选项应在其他下拉列表中禁用。场景 2 - 如果选择被更改,它也应该在其他下拉菜单中重新启用。希望你明白了。
【解决方案3】:

看看

               <form id="form1">
        <select id="select1">
         <option     value="Question1">Question 1</option>
            <option value="Question2" >Question 2</option>
            <option  value="Question3"  >Question 3</option>
            <option  value="Question4"  >Question 4</option>
        </select>
<select id="select2">
         <option     value="Question1">Question 1</option>
            <option value="Question2" >Question 2</option>
            <option  value="Question3"  >Question 3</option>
            <option  value="Question4"  >Question 4</option>
        </select>

    <select id="select3">
         <option     value="Question1">Question 1</option>
            <option value="Question2" >Question 2</option>
            <option  value="Question3"  >Question 3</option>
            <option  value="Question4"  >Question 4</option>
        </select>




     </form>





               $(document).on('change','select',function(e){
                     var elm = $(this);
                     elm.find('option').show();
                     $('select').not(elm).find('option',each(function(){
                        if($(this).attr('value')==$(elm).val()){
                             $(this).hide();
                         }else{
                                 $(this).show();
                         }
                     }));



                });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 2015-09-19
    • 1970-01-01
    • 2016-02-21
    • 2017-12-16
    • 1970-01-01
    相关资源
    最近更新 更多