简体   繁体   English

如何使用jQuery检测选项取消选择?

[英]How to detect option deselect with jquery?

I have a list of options. 我有一个选项列表。 When selectting an option in the list I create new elemets and append them to another div. 在列表中选择一个选项时,我将创建新的图元并将它们附加到另一个div。

HTML: HTML:

<select multiple="multiple">
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select>

<div class="tankListProduct">
  // things get appended here
</div>

Script 脚本

$(function(){

    $("#SelectedProduktValues")
        .on("change", function(e) {
            var optionValueArray = $(this).val();
            var optionValue = optionValueArray[optionValueArray.length-1];
            var optionText = $("#SelectedProduktValues option[value='" + optionValue + "']").text();

            var $options = $("#SelectedTanksValues > option").clone();

            var div = "<div class='col-xs-20 adminRow'>";
            div += "<p class='col-xs-20 text-center'>" + optionText + "</p>";
            div += "<select class='form-control tankList'</select>";
            div += "</div>";

            $(".tankListProduct").append(div);

            $('.tankList').last().selectpicker();
        });
})

This works perfectly and is shown in the fiddle below but I can't figre out how I can detect if an option in the list has been deselected so that I can remove the inserted element that corresponds to that option? 这可以正常工作,并显示在下面的小提琴中,但我不知道如何检测列表中的某个选项是否已取消选中,以便可以删除与该选项相对应的插入元素?

Fiddle 小提琴

EDIT: 编辑:

If it was uncelar, this is a multiSelect list 如果不是,这是一个multiSelect列表

on onchange event you can just clearout the existing html and add new contents of the selected option 在onchange事件上,您可以清除现有的html并添加所选选项的新内容

check this snippet 检查此片段

 $(function() { $("#SelectedProduktValues") .on("change", function(e) { var optionValueArray = $(this).val(); var optionValue = optionValueArray[optionValueArray.length - 1]; var options = $("#SelectedProduktValues").find('option'); var element = $(".tankListProduct"); var elem = $(element[0]); options.each(function(option) { var isSelected = $(options[option]).prop('selected'); var optionText = $(options[option]).text(); var child = "div#" + optionText; if (isSelected) { var $options = $("#SelectedTanksValues > option").clone(); if (elem.find(child).length == 0) { var div = "<div class='col-xs-20 adminRow' id='" + optionText + "'>"; div += "<p class='col-xs-20 text-center'>" + optionText + "</p>"; div += "<select class='form-control tankList'</select>"; div += "</div>"; $(".tankListProduct").append(div); } } else { $(".tankListProduct").find("div#" + optionText).remove(); } }) }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <select id="SelectedProduktValues" multiple="multiple"> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select> <div class="tankListProduct"> // things get appended here </div> 

Hope it helps 希望能帮助到你

  1. You forgot to close the select tag. 您忘记了关闭选择标签。
  2. Clearing the html before you append should help "deselect" the no longer selected ones. 在追加之前清除html应该有助于“取消选择”不再选择的内容。
  3. And a jQuery each loop to loop over the selected elements. 还有一个jQuery每个循环,以循环显示所选元素。
  4. I've added a button to clear the selected items, if you dont want anything selected. 如果您不想选择任何内容,我添加了一个按钮以清除选定的项目。 I'm sure you can come up with something more clever though. 我相信您可以提出更聪明的建议。

HTML HTML

<select id="SelectedProduktValues" multiple="multiple">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>

<div class="clearList"> Click to clear selction </div>

<div class="tankListProduct">

</div>

Javscript/JQuery Javscript / JQuery的

$(function() {

  $("#SelectedProduktValues")
    .on("change",
      function(e) {

        var optionValueArray = $(this).val();


        var div = '';
        $.each(optionValueArray, function(key, value) {
          var optionValue = value;
          var optionText = $("#SelectedProduktValues option[value='" + optionValue + "']").text();

          div += "<div class='col-xs-20 adminRow'>";
          div += "<p class='col-xs-20 text-center'>" + optionText + "</p>";
          div += "<select class='form-control tankList'> </select>";
          div += "</div>";
        });

        $(".tankListProduct").html('');
        $(".tankListProduct").append(div);

        $('.tankList').last().selectpicker();

      });
})

$(".clearList").on("click", function(e) {
  $(".tankListProduct").html('');
});

And here's a fiddle. 这是一个小提琴。

https://jsfiddle.net/uhnkwx1g/8/ https://jsfiddle.net/uhnkwx1g/8/

Hope this helps. 希望这可以帮助。

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

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