Basically I want to loops through all the inputs that has a "data-id" attribute and with a matching ItemID, then insert them into the database in order. Unfortunately they're not being inserted into the database in the correct order.
Here is my html:
<input id="control01" data-ItemId="11" value="xxx">
<input id="control02" data-ItemId="11" value="xxx">
<input id="control03" data-ItemId="12" value="xxx">
<input id="control04" data-ItemId="12" value="xxx">
<input id="control05" data-ItemId="12" value="xxx">
<input id="control06" data-ItemId="13" value="xxx">
<input id="control07" data-ItemId="13" value="xxx">
<input id="control08" data-ItemId="13" value="xxx">
<input id="control09" data-ItemId="14" value="xxx">
<input id="control10" data-ItemId="14" value="xxx">
<input id="control11" data-ItemId="14" value="xxx">
Here's my code:
var ItemIDs = [12,13,14]; //Here's the given ID we're looking for
$.each(ItemIDs, function (index, value) {
//alert(value); If I add an alert here it'll be inserted in the correct order
$(":input[data-id='" + value + "']").each(function (index2, value2) {
//Code to insert its value into SQL database;
//PageMethods.AddItemToDB(ItemID);
});
});
I want the result in the database to be:
AutoID ItemID
1 12
2 12
3 12
4 13
5 13
6 13
7 14
8 14
9 14
But here's what I'm getting:
AutoID ItemID
1 12
2 12
3 13
4 14
5 14
6 13
7 14
8 12
9 13
I have no clue why the order is so random. I tried adding an alert inside each loop, which to my surprise actually fixed the problem, otherwise it'll just be in random order. What am I doing wrong here? I'll greatly appreciate any help or suggestion!
$.eachon"12 13 14"is only going to iterate once, since that's not an array. Should be[12, 13, 14]