admin管理员组

文章数量:1418017

I'm trying to append the values of array I've made into another div but when I append the array value its appending all the values of the array not just the one I've added(by click), any suggestions would be great, thanks in advance!

HTML

<div id="products">
    <ul>
        <li><p>Lorem 1</p><button class="target">Add</button></li>
        <li><p>Lorem 2</p><button class="target">Add</button></li>
    </ul>
</div>
<div id="cart">
    <ul>

    </ul>
</div>

jQuery

$(document).ready(function(){

    var array = Array();

    $(".target").click(function() {

        array.push($(this).siblings('p').text());

        $.each(array, function(index, value) {

            $("#cart ul").append("<li>"+value+"</li>");
        });


    });
});

When I click Add on say the first button its displays

Lorem 1

but when I then click the add on the second button it displays

Lorem 1 Lorem 1 Lorem 2

I'm trying to append the values of array I've made into another div but when I append the array value its appending all the values of the array not just the one I've added(by click), any suggestions would be great, thanks in advance!

HTML

<div id="products">
    <ul>
        <li><p>Lorem 1</p><button class="target">Add</button></li>
        <li><p>Lorem 2</p><button class="target">Add</button></li>
    </ul>
</div>
<div id="cart">
    <ul>

    </ul>
</div>

jQuery

$(document).ready(function(){

    var array = Array();

    $(".target").click(function() {

        array.push($(this).siblings('p').text());

        $.each(array, function(index, value) {

            $("#cart ul").append("<li>"+value+"</li>");
        });


    });
});

When I click Add on say the first button its displays

Lorem 1

but when I then click the add on the second button it displays

Lorem 1 Lorem 1 Lorem 2

Share Improve this question edited Mar 25, 2013 at 17:17 Rob 4,94712 gold badges52 silver badges56 bronze badges asked Mar 25, 2013 at 16:33 user2201765user2201765 1,0436 gold badges18 silver badges21 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 1

You are adding the items to the array, which makes sense if you want to keep a list of added items, but then you are looping through the array when adding items to the list, which will make it add all items to the items already existing there.

You can just add the last item:

array.push($(this).siblings('p').text());
$("#cart ul").append("<li>" + array[array.length - 1] + "</li>");

or you can clear the list before adding the items:

array.push($(this).siblings('p').text());
$("#cart ul").empty();
$.each(array, function(index, value) {
  $("#cart ul").append("<li>" + value + "</li>");
});

Because you loop on each element of the array, so you get all your elements. Try without the $.each in your click function, it will be better.

If you want to only add one value when the selector $(".target") is clicked, you should do something like

  // assuming you want the last value in the array.
  var value = array[array.length-1]; // could also do $(this).siblings('p').text()
  $("#cart ul").append("<li>"+value+"</li>");

It seems like you might not even need your array, (unless it is being used by some other functions as well).

LIVE DEMO

$(function(){

  var array = [];

  $(".target").click(function() {

     array.push( $(this).siblings('p').text() );
     $("#cart ul").append("<li>"+ array[array.length -1] +"</li>");

  });

});

本文标签: javascriptjQuery each array index append to divStack Overflow