执行数学我有如下表jQuery来通过每个表行循环,并与每行输入字段
<table class="authors-list" border=0 id="ordertable">
<tr>
<td>Cubic Meters</td><td>Cubes per Bundle</td><td>Total Bundles</td>
<tr>
<td><input type="text" name="cubicmeters1" id="cubicmeters1" value="1.38"></td>
<td><input type="text" name="cubesperbundle1" id="cubesperbundle1" value="1.485"></td>
<td><input type="text" name="bundles1" id="bundles1"></td>
</tr>
<tr>
<td><input type="text" name="cubicmeters2" id="cubicmeters2" value="1.38"></td>
<td><input type="text" name="cubesperbundle2" id="cubesperbundle2" value="1.485"></td>
<td><input type="text" name="bundles2" id="bundles2"></td>
</tr>
<tr>
<td><input type="text" name="cubicmeters3" id="cubicmeters3" value="1.38"></td>
<td><input type="text" name="cubesperbundle3" id="cubesperbundle3" value="1.485"></td>
<td><input type="text" name="bundles3" id="bundles3"></td>
</tr>
</table>
我想要做的是循环遍历每个表行和反对两个输入执行数学函数,并将结果填充到第三个输入中。
小提琴是在这里:http://jsfiddle.net/ttZtX/1/
因此,这里是我jQuery的尝试:
$("table.authors-list").find('input[name^="cubicmeters1"]').each(function() {
cubes += +$(this).closest('tr').find('input[name^="cubicmeters"]').val();
cubesperbundle += +$(this).closest('tr').find('input[name^="cubesperbundle"]').val();
+$(this).closest('tr').find('input[name^="bundles"]').val((cubes*cubesperbundle).toFixed(2));
});
这根本不会产生任何结果或错误,不填充输入。总结一下,我的要求是让jquery循环遍历每一行,将cubicmeters
乘以cubesperbundle
,然后填充bundles
并返回结果。
够简单,但我只是不能正确理解。
任何帮助,将不胜感激。
在此先感谢...
谢谢穆萨。如果在身体本身,这可以工作。麻烦的是立方米和cubesperbundle由一个较早的jquery函数填充,这是在体内运行,所以我需要在输入填充后触发该脚本。谢谢 – Smudger 2013-04-25 06:40:23
@Smudger代码在哪里并不重要,它在调用时很重要。你需要在dom加载完成后调用它(或者只是在加载元素时加载) – Musa 2013-04-25 06:44:02
谢谢。这是目前所有其他脚本的正文。我如何实现这一目标?其他输入由这个脚本填充:'for(var i = 1; i <= rows; i ++){function(index){'0'post('get_sku_pieces',{ data:$('#product '+指数).VAL() },功能(结果){ $(' #pcsperbundle” +指数).VAL(结果); });} )(ⅰ) }' – Smudger 2013-04-25 06:49:40