如何删除我从数组中选择的元素

时间:2018-06-25 09:52:24

标签: javascript arrays

// Added during edit. This was not in OP's original code
var estate = [];

$("#select-estate").change(function() {
  var singleValues = $("#select-estate").val();
  if (estate.includes(singleValues) == false) {
    estate.push(singleValues);
    $("span.filter-push-select").append('<label class="active">' + singleValues + '<i class="fa fa-times fa-lg" aria-hidden="true"></i></label>');
    $("span.filter-push-select label").click(function() {
      console.log(singleValues);
      console.log(estate);
      $(this).remove();
      estate.splice($("#select-estate").val(), 1);
      console.log(estate);
    });
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<select name="increase" class="form-control" id="select-estate">
  <option disabled selected value="Select">Estate Type</option>
  <option value="ownership">ownership</option>
  <option value="rent">Rent</option>
</select>
<span class="filter-push-select"></span>

我使用遗产数组并从选择值html中提取一个值以附加它,然后单击以删除我需要的内容。从数组和UI Append中删除此函数不正确

1 个答案:

答案 0 :(得分:0)

您要检查包含在我已添加到字符串进行检查的数组中的检查解决方案

var estate=[];
$("#select-estate").change(function() {
  var singleValues = $("#select-estate").val();
  if (estate.toString().includes(singleValues) == false) {
    estate.push(singleValues);
    $("span.filter-push-select").append('<label class="active" style="cursor:pointer">' + singleValues + '<i class="fa fa-times fa-lg" aria-hidden="true"></i></label><br/>');
    $("span.filter-push-select label").click(function() {
      console.log(singleValues);
      console.log(estate);
      $(this).remove();
      estate.splice($("#select-estate").val(), 1);
      console.log(estate);
    });
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="increase" class="form-control" id="select-estate">
       <option disabled selected value="Select">Estate Type</option>
       <option value="ownership">ownership</option>
       <option value="rent">Rent</option>
</select>
<br/>
<div>
<span class="filter-push-select"></span>
</div>