0
我甚至不知道如何解釋我面臨的問題,因爲這很奇怪。jQuery DOM元素值神祕減少
我有一個按鈕單擊事件如下面的代碼
$('body').on('click', '#btnAddCart', function(e){
e.preventDefault();
if ($(":checkbox[name='app_chk']", '#apps_container').is(":checked"))
{
$('#alert').html("");
console.log(localGet('cart'));
var arr = [];
var items = [];
var subtotal = $('#apps_subtotal').text().trim();
var total = $('#apps_total').text().trim();
var cart = localGet('cart');
var flag = 0;
var app_id, index, price, name, i_parent, i_parent_name;
$(":checkbox[name='app_chk']:checked").each(function() {
index = $('[name=app_chk]').index(this);
price = $('[name=app_price]:eq('+index+')').val();
name = $('[name=app_name]:eq('+index+')').val();
i_parent = $('[name=i_parent]:eq('+index+')').val();
i_parent_name = $('[name=i_parent_name]:eq('+index+')').val();
// app_id = $('[name=app_id]:eq('+index+')').val();
app_id = $('[name=app_id]:eq('+index+')').val();
console.log('index: ' + index);
console.log('id: ' + app_id);
console.log('i_parent: ' + i_parent);
var position = checkedInLocalStorage('cart', app_id);
if(cart !== null){//the item with the same id exists
console.log("pos: "+position);
if(position > -1){
//perform update
cart[0].items[position].id = app_id;
cart[0].items[position].price = price;
cart[0].items[position].name = name;
cart[0].items[position].i_parent = i_parent;
cart[0].items[position].i_parent_name = i_parent_name;
}else{
cart[0].items.push({
id : app_id,
i_parent : i_parent,
price : price,
name : name,
i_parent_name : i_parent_name
});
flag = 1;
}
}else{
items.push({
id : app_id,
i_parent : i_parent,
price : price,
name : name,
i_parent_name : i_parent_name
})
}
});
if(cart === null) {
arr.push({
items: items,
subtotal: subtotal,
total: total
});
console.log(arr);
localStore('cart', arr);
}else{
cart[0].subtotal = subtotal;
cart[0].total = total;
localStore('cart', cart);
console.log(cart);
}
displayCart();
$('#cart_wrapper').addClass('visible');
localStorage.removeItem('apps');
}
else
{
showError('Please select at least one app to add to cart');
}
});
我不知道爲什麼app_id
對同一對象的值由一個每個按鈕btnAddCart
點擊其他的同時該數值時間減少元素很好,不會改變。沒有任何操作會更改代碼中的app_id
值。
我還附上測試日誌,以顯示相同索引的app_id
的值神奇地變化。
有誰知道是什麼原因造成的問題?