Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Jquery中迭代表以更改内容_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何在Jquery中迭代表以更改内容

Javascript 如何在Jquery中迭代表以更改内容,javascript,jquery,html,Javascript,Jquery,Html,我有这把小提琴 如果输入类型有两个按钮+和-,我想将输入值添加到表中的所有单位值中 JQuery看起来像这样 $(".commission").TouchSpin({ min: 0, max: 2, step: 0.0001, decimals: 4, boostat: 5, maxboostedstep: 10, buttondown_class: 'btn btn-white', buttonup_class: 'btn btn-white' }); $("

我有这把小提琴

如果输入类型有两个按钮
+
-
,我想将输入值添加到表中的所有单位值中

JQuery看起来像这样

$(".commission").TouchSpin({
  min: 0,
  max: 2,
  step: 0.0001,
  decimals: 4,
  boostat: 5,
  maxboostedstep: 10,
  buttondown_class: 'btn btn-white',
  buttonup_class: 'btn btn-white'
});
$(".commission").on('touchspin.on.startupspin', function() {
  var table = $(this).parent().parent().parent().prev().find('table');
  console.log(table.rows);
})

你可以在fiddle本身看到的HTML我不知道你使用的插件,所以我将把我的函数放在一个
.change()
中,但是把这个代码放在每次输入更改时调用的函数中:

$('body').on('change','.commission',function(){
  var $this = $(this);
  $('.unit').each(function(){
    $(this).text(+$(this).text()+$this.val());
  });
});
我已使用以下代码更新了您的:

$(".commission").on('touchspin.on.stopspin', function() {
  var cells = $(this).parent().parent().parent().parent().find('table.cust-tbl td');
  for (var i = 0; i < cells.length; i++) {
    var $cell = cells.eq(i);
    var num = $cell.data('num');
    if (num === undefined || num == null) {
      num = $cell.text().match(/[\d.]+/);
      if (num) {
        $cell.data('num', num[0]);
      }
    }
    num = +$cell.data('num');
    if (num) {
      num += (+$(this).val());
      $cell.text($cell.text().replace(/[\d.]+/, num.toFixed(4)));
    }
  }
});
$(.commission”).on('touchspin.on.stopspin',function(){
变量单元格=$(this.parent().parent().parent().parent().find('table.cust-tbl td');
对于(变量i=0;i
此代码将原始值存储在单元格的
data
属性中,并将值添加到该属性中

还将事件更改为
stopspin
,以便在选择完值(向上或向下)后触发


添加了四舍五入到4位小数
。toFixed(4)

页面中有多少表格,我应该如何只关注最靠近
.commission
class@Vikram将表格单元格的选择器更改为该表格的唯一选项。->
表td
您可以将
:contains(“p/kWh”)
添加到选择器:
'table td:contains(“p/kWh”)
@Vikram您在选择器中缺少
td
。@Vikram使用逗号
分隔选择器:
$(this.parent().parent().parent().parent().find().find('table td:contains(“p/kWh”)),table td:contains(“p/kWh”)你能编辑小提琴本身并让我知道吗,我所要做的就是把佣金的价值加到表中的单位价值上