dongzhan7253 2016-02-28 15:03
浏览 52
已采纳

如何使用Jquery获取多个数组并更改其相关数组键的值

This is the sample image of the output. What I want is if I change the quantity in that column its co-related total-price will change also. I want to achieve this injquery yet Im struggling.This is my current code and it still has error if i update the quantity all the total price will change which is wrong I want to only change the total price of the edited column.

var delay = (function(){
    var timer = 0;
    return function(callback, ms){
      clearTimeout (timer);
      timer = setTimeout(callback, ms);
    };
  })();

  $(".qty-mod").keyup(function(){
          var qty = $(this).val();
          delay(function(){
          var newprice = qty * totalorgprice
          $(".price-total").val(newprice);
          console.log(qty);
      }, 1000);
  });  

enter image description here

  • 写回答

1条回答 默认 最新

  • doufei0933 2016-02-28 16:29
    关注

    You've bound all .qty-mod at once, and this is good.

    But when in the keyup() function you're operating on only one .qty-mod, so should update only the corresponding .price-total.
    And currently you're explicitly updating all of them!

      $(".qty-mod").keyup(function(){
          var qty = $(this).val();
          var newprice = qty * totalorgprice;
          $(<< here should refer to the involved .price-total only >>).val(newprice);
          console.log(qty);
      });
    

    Since you didn't show your HTML code I can only guess that your data is displayed in a <table>. If so, the << here ... only >> above might be something like:

    '.price-total', $(this).closest('tr')
    

    Explained for the case you're not easy with jQuery:

      '.price-total',    // an element with class "price-total"
      $(this)            // in the context of 
        .closest('tr')   // the <tr> parent of the [$(this)] current element
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。