var items=[];
items.push(data);
I am using an array to store my data, and then submit using ajax. For the first time submission, it works fine. But when submit subsequently, it will submit null value together with data passing to server.
I am using the method below to clear the array, but i found that the previous data just set to null. For example, for the first time i passed value 1, successful. For the second submission, i clear the array and store new value, e.g. i pushed value 2 to the items[], when submit it will submit null and 2.
items.splice(0);
How can i fix it? I need to reset the array every time submit the value.
Code:
var items=[];
$("#selection").click(function(e) {
$(":checkbox").each(function() {
if(this.checked === false) {
this.checked = true;
items.push(this.id);
}
});
});
$('#grid').submit(function(e){
e.preventDefault();
var formdata = {
data:items
};
$.ajax({
type: "POST",
url: "/add",
data: formdata ,
dataType:'html',
success: function(data) {
alert("added");
}
});
});
items.length = 0or just reassign a new, empty arrayitems = []. (Regardless, this isn't a jQuery problem, it's a plain JavaScript thing.)items.push()from inside a click handler seems a bit strange - wouldn't that mean that the same values get added to the array multiple times if the user clicks that element multiple times before submitting?