如何在克隆的<select>上删除所选选项,并在选择新选项时恢复先前选择的选项

时间:2017-10-04 05:48:21

标签: javascript jquery html

如何克隆&lt; select&gt;到另外两个&lt; select&gt;并隐藏在其他&lt; select&gt;上选择的选项并在选择新选项时显示隐藏选项。我也希望它与浏览器兼容。 在这里,我有三个下拉列表显示业务类别,所有三个下拉列表具有相同的值。我想隐藏并显示下拉菜单上的选项,在其他下拉列表中选择的选项。例如,如果我选择艺术,工艺品和收藏品下拉1,它必须隐藏在下拉2和下拉3,当我选择宝贝下拉1然后艺术,工艺品和收藏品必须显示和婴儿必须隐藏在下拉2和下拉3 &#13; &#13; categories = [&#13; {catValue:1,catName:&#39;艺术,手工艺品和收藏品&#39;},&#13; {catValue:2,catName:&#39; Baby&#39;},&#13; {catValue:3,catName:&#39;美容和香水&#39;},&#13; {catValue:4,catName:&#39;书籍和杂志&#39;},&#13; {catValue:5,catName:&#39;企业对企业&#39;},&#13; {catValue:6,catName:&#39;服装,配饰和鞋子&#39;},&#13; {catValue:7,catName:&#39;古董&#39;},&#13; {catValue:8,catName:&#39;艺术和工艺用品&#39;},&#13; {catValue:9,catName:&#39;艺术品经销商和画廊&#39;},&#13; {catValue:10,catName:&#39;相机和摄影用品&#39;},&#13; {catValue:11,catName:&#39;数字艺术&#39;},&#13; {catValue:12,catName:&#39; Memorabilia&#39;}&#13; ];&#13; &#13; var categoriesJson = JSON.stringify(categories);&#13; &#13; $(document).ready(function(){&#13; $(&#39; .cat2&#39)。隐藏();&#13; $(&#39; .cat3&#39)。隐藏();&#13; &#13; $ .each(JSON.parse(categoriesJson),function(key,value){&#13; $(&#34;#类别1&#34)。追加($(&#34;&LT;选项&GT;&LT; /选项&GT;&#34;)VAL(value.catValue)的.html(value.catName)) ;&#13; });&#13; &#13; $(&#34; #category2&#34;)。html($(&#34; #categ1&#34;)。html());&#13; $(&#34; #category3&#34;)。html($(&#34; #categ1&#34;)。html());&#13; &#13; $(&#34; #categ1&#34;)。change(function(){&#13; var cat1Value = $(this).val();&#13; $(&#39; .cat2&#39)。显示();&#13; $(&#34; #category2选项[value =&#34; + cat1Value +&#34;]&#34;)。hide();&#13; $(&#34; #category3选项[value =&#34; + cat1Value +&#34;]&#34;)。hide();&#13;     });&#13; &#13;     $(&#34; #category2&#34;)。change(function(){&#13; var cat1Value = $(this).val();&#13; $(&#39; .cat3&#39)。显示();&#13; $(&#34;#category1选项[value =&#34; + cat1Value +&#34;]&#34;)。hide();&#13; $(&#34; #category3选项[value =&#34; + cat1Value +&#34;]&#34;)。hide();&#13;     });&#13; &#13;     $(&#34; #category3&#34;)。change(function(){&#13; var cat1Value = $(this).val();&#13; $(&#34; #category2选项[value =&#34; + cat1Value +&#34;]&#34;)。hide();&#13; $(&#34;#category1选项[value =&#34; + cat1Value +&#34;]&#34;)。hide();&#13;     });&#13; &#13; });&#13; &lt; script src =&#34; https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt; / script&gt;&#13; &#13; &lt; div class =&#34; cat1&#34;&gt;&#13;     &lt; label&gt;类别1&lt; / label&gt;&#13;     &lt; select id =&#34; category1&#34;名称=&#34; businessCategory&#34;&GT;&#13;         &lt; option&gt;选择类别&lt; / option&gt;&#13;     &LT; /选择&GT;&#13; &LT; / DIV&GT;&#13;     &#13; &lt; div class =&#34; cat2&#34;&gt;&#13;     &lt; label&gt;类别2&lt; / label&gt;&#13;     &lt; select id =&#34; category2&#34;&gt;&#13;     &LT; /选择&GT;&#13; &LT; / DIV&GT;&#13; &#13; &lt; div class =&#34; cat3&#34;&gt;&#13;     &lt; label&gt;类别3&lt; / label&gt;&#13;     &lt; select id =&#34; category3&#34;&gt;&#13;     &LT; /选择&GT;&#13; &LT; / DIV&GT;&#13; &#13; &#13;

2 个答案:

答案 0 :(得分:2)

我希望这可以帮助您完成您的工作。

categories = [{
    catValue: 1,
    catName: 'Arts, crafts, and collectibles'
  },
  {
    catValue: 2,
    catName: 'Baby'
  },
  {
    catValue: 3,
    catName: 'Beauty and fragrances'
  },
  {
    catValue: 4,
    catName: 'Books and magazines'
  },
  {
    catValue: 5,
    catName: 'Business to business'
  },
  {
    catValue: 6,
    catName: 'Clothing, accessories, and shoes'
  },
  {
    catValue: 7,
    catName: 'Antiques'
  },
  {
    catValue: 8,
    catName: 'Art and craft supplies'
  },
  {
    catValue: 9,
    catName: 'Art dealers and galleries'
  },
  {
    catValue: 10,
    catName: 'Camera and photographic supplies'
  },
  {
    catValue: 11,
    catName: 'Digital art'
  },
  {
    catValue: 12,
    catName: 'Memorabilia'
  }
];

var categoriesJson = JSON.stringify(categories);

$(document).ready(function() {
  $('.cat2').hide();
  $('.cat3').hide();

  $.each(JSON.parse(categoriesJson), function(key, value) {
    $("#category1").append($("<option></option>").val(value.catValue).html(value.catName));
  });

  $("#category2").html($("#category1").html());
  $("#category3").html($("#category1").html());

  $("#category1").change(function() {
    var cat1Value = $(this).val();
    $('.cat2').show();
    if (cat1Value == $("#category2").val()) {
      $("#category2").val($("#category2 option:first").val());
    }
    if (cat1Value == $("#category3").val()) {
      $("#category3").val($("#category3 option:first").val());
    }
    $("#category2 option").show();
    $("#category3 option").show();
    $("#category2 option[value=" + cat1Value + "]").hide();
    $("#category3 option[value=" + cat1Value + "]").hide();
  });

  $("#category2").change(function() {
    $('.cat3').show();
    // var cat1Value = $(this).val();
    // $("#category1 option[value=" + cat1Value + "]").hide();
    // $("#category3 option[value=" + cat1Value + "]").hide();
  });

  $("#category3").change(function() {
    // var cat1Value = $(this).val();
    // $("#category2 option[value=" + cat1Value + "]").hide();
    // $("#category1 option[value=" + cat1Value + "]").hide();
  });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<div class="cat1">
  <label>Category 1</label>
  <select id="category1" name="businessCategory">
        <option>Select category</option>
    </select>
</div>
<div class="cat2">
  <label>Category 2</label>
  <select id="category2">
    </select>
</div>
<div class="cat3">
  <label>Category 3</label>
  <select id="category3">
    </select>
</div>

答案 1 :(得分:1)

这是你正在寻找的吗?

function hide() {
  selected = $("select[id^=category]").map(function() {
    if ($(this).find(":selected").val() != "Select category") {
      return $(this).find(":selected").text()
    }
  })
  $("select[id^=category]").each(function() {
    $(this).find("option").show();
    $(this).find('option').filter(function() {
      return $.inArray($.trim($(this).text()), selected) > -1;
    }).hide();
  })
}

我还缩短了你的代码,使其更干净。

<强>演示

&#13;
&#13;
categories = [{
    catValue: 1,
    catName: 'Arts, crafts, and collectibles'
  },
  {
    catValue: 2,
    catName: 'Baby'
  },
  {
    catValue: 3,
    catName: 'Beauty and fragrances'
  },
  {
    catValue: 4,
    catName: 'Books and magazines'
  },
  {
    catValue: 5,
    catName: 'Business to business'
  },
  {
    catValue: 6,
    catName: 'Clothing, accessories, and shoes'
  },
  {
    catValue: 7,
    catName: 'Antiques'
  },
  {
    catValue: 8,
    catName: 'Art and craft supplies'
  },
  {
    catValue: 9,
    catName: 'Art dealers and galleries'
  },
  {
    catValue: 10,
    catName: 'Camera and photographic supplies'
  },
  {
    catValue: 11,
    catName: 'Digital art'
  },
  {
    catValue: 12,
    catName: 'Memorabilia'
  }
];

var categoriesJson = JSON.stringify(categories);

$(document).ready(function() {
  $('.cat2').hide();
  $('.cat3').hide();

  $.each(JSON.parse(categoriesJson), function(key, value) {
    $("#category1").append($("<option></option>").val(value.catValue).html(value.catName));
  });

  $("#category2").html($("#category1").html());
  $("#category3").html($("#category1").html());

  $("#category1").change(function() {
    $('.cat2').show();
    hide()
  });

  $("#category2").change(function() {
    $('.cat3').show();
    hide()
  });

  $("#category3").change(function() {
    hide()
  });

  var selected = [];

  function hide() {
    selected = $("select[id^=category]").map(function() {
      if ($(this).find(":selected").val() != "Select category") {
        return $(this).find(":selected").text()
      }
    })
    $("select[id^=category]").each(function() {
      $(this).find("option").show();
      $(this).find('option').filter(function() {
        return $.inArray($.trim($(this).text()), selected) > -1;
      }).hide();
    })
  }

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<div class="cat1">
  <label>Category 1</label>
  <select id="category1" name="businessCategory">
        <option>Select category</option>
    </select>
</div>

<div class="cat2">
  <label>Category 2</label>
  <select id="category2">
    </select>
</div>

<div class="cat3">
  <label>Category 3</label>
  <select id="category3">
    </select>
</div>
&#13;
&#13;
&#13;

<强> DEMO2

&#13;
&#13;
categories = [{
    catValue: 1,
    catName: 'Arts, crafts, and collectibles'
  },
  {
    catValue: 2,
    catName: 'Baby'
  },
  {
    catValue: 3,
    catName: 'Beauty and fragrances'
  },
  {
    catValue: 4,
    catName: 'Books and magazines'
  },
  {
    catValue: 5,
    catName: 'Business to business'
  },
  {
    catValue: 6,
    catName: 'Clothing, accessories, and shoes'
  },
  {
    catValue: 7,
    catName: 'Antiques'
  },
  {
    catValue: 8,
    catName: 'Art and craft supplies'
  },
  {
    catValue: 9,
    catName: 'Art dealers and galleries'
  },
  {
    catValue: 10,
    catName: 'Camera and photographic supplies'
  },
  {
    catValue: 11,
    catName: 'Digital art'
  },
  {
    catValue: 12,
    catName: 'Memorabilia'
  }
];

var categoriesJson = JSON.stringify(categories);

$(document).ready(function() {
  $('.cat2').hide();
  $('.cat3').hide();

  $.each(JSON.parse(categoriesJson), function(key, value) {
    $("#category1").append($("<option></option>").val(value.catValue).html(value.catName));
  });

  $("#category2").html($("#category1").html());
  $("#category3").html($("#category1").html());

  $("#category1").change(function() {
    $('.cat2').show();
    hide()
  });

  $("#category2").change(function() {
    $('.cat3').show();
    hide()
  });

  $("#category3").change(function() {
    hide()
  });

  var selected = [];

  function hide() {
    selected = $("select[id^=category]").map(function() {
      if ($(this).find(":selected").val() != "Select category") {
        return $(this).find(":selected").val()
      }
    })
    $("select[id^=category]").each(function() {
      $(this).find("option").show();
      $(this).find('option').filter(function() {
        return $.inArray($.trim($(this).val()), selected) > -1;
      }).hide();
    })
  }

});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<div class="cat1">
  <label>Category 1</label>
  <select id="category1" name="businessCategory">
        <option>Select category</option>
    </select>
</div>

<div class="cat2">
  <label>Category 2</label>
  <select id="category2">
    </select>
</div>

<div class="cat3">
  <label>Category 3</label>
  <select id="category3">
    </select>
</div>
&#13;
&#13;
&#13;