2016-05-30 120 views
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的值神奇地变化。

有谁知道是什么原因造成的问题?

testing log

回答

0

这似乎是可变APP_ID是保留变量。我将dom的名称和变量名更改为app_idx