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 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题