选择时删除选择列表选项

时间:2018-07-03 20:08:04

标签: javascript html arrays onchange

我想创建一个选择选项列表,该列表会在选择选项时动态删除它们。第一次选择后,列表不再响应。任何帮助深表感谢!

更新正常):
替换为:$(“。select_list”)。change(function(){
使用:$('#menu')。on(“ click”,'.select_list',function(){

HTML:

var misc_array = ["A", "B", "C"];

function buildList() {
  var select_list = "<select class='select_list'>";
  select_list += "<option>Select</option>";

  for (i = 0; i < misc_array.length; i++) {
    select_list += "<option>" + misc_array[i] + "</option>";
  }

  select_list += "</select>";
  return select_list;
}

$("#menu").html(buildList());

$(".select_list").change(function() {
  console.log(this.value);
  for (i = 0; i < misc_array.length; i++) {
    if (misc_array[i] == this.value) {
      misc_array.splice(i, 1);
    }
  }
  // $("#menu").empty();
  $("#menu").html(buildList());
  console.log(misc_array);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body>

  <div id="menu"></div>

</body>

0 个答案:

没有答案