这是html:

<button id="btn1">CLICK 1</button>
<button id="btn2">CLICK 2</button>
<div id="show"></div>


这是javascript:

product_id = new Array();
$("#btn").on('click', function () {
    $("#show").append("<div><button type='button' id='btn1x' class='close pull-right' aria-hidden='true'>&times;</button>" + "<pre>button 1</pre></div>");

    product_id.push("btn1");
    alert(product_id);
});

$(document).on('click', 'button#btn1x', function () {
    $(this).parent().remove();
    alert(product_id);
    //when I click this button, i want to remove the "btn1" that I pushed a while ago from my array
});


$("#btn2").on('click', function () {
    $("#show").append("<div><button type='button' id='btn2x' class='close pull-right' aria-hidden='true'>&times;</button>" + "<pre>button 2</pre></div>");

    product_id.push("btn2");

});

$(document).on('click', 'button#btn2x', function () {
    $(this).parent().remove();
    //when I click this button, i want to remove the "btn2" that I pushed a while ago from my array
});


我想单击该按钮,最后将某个值插入到我创建的数组中。但是,我还创建了一个关闭按钮,当我单击它时,我想从数组中删除插入的值。

最佳答案

使用.pop(),所有常规的javascript函数都可在jQuery中使用。
http://learn.jquery.com/javascript-101/arrays/

给定一个数组:

var array = [1,2,3,5];
array.push(9); // [1,2,3,5,9]
array.pop(); // [1,2,3,5]


现在,如果您想删除2 ...那就不一样了。

关于javascript - 如何使用JavaScript删除数组中的项目?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19391999/

10-11 22:10
查看更多