简体   繁体   中英

How Can I remove element i selected from array

 // 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> 

I use estate array and pull a value from select value html to append it and click to remove what I need. this function not true in removing from array and UI Append

Check solution you were trying to check include on array I have added to string for check

 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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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