[英]Can't get the value of closest prev td input
I need your help! 我需要你的帮助!
I have fiddle and what I want is multiplicate two td's values and the result go to the next td. 我有小提琴 ,我想要的是将两个td的值相乘,结果转到下一个td。
$(document).ready(function(){
$( "input[id^='unitval']" ).keyup(function() {
var input_value = parseFloat($(this).val());
var Cant = $("#item_Cant").text();
var totval = (input_value * Cant);
if (!isNaN(input_value)) { // the input is a number
//$("#totval1").val(totval); // update second field
$(this).closest('td').next().find('input').val(totval);
} else { // the input wasn't a number
$("#totval1").val("not a number?"); // show an error mesage
}
});
});
But just works with the first row because the second one still multiplicating with the td of first row not the actual td 但只适用于第一行,因为第二行仍与第一行的td相乘,而不是实际的td
I tried to change the: 我试图更改:
var Cant = $("#item_Cant").text();
with 与
var Cant = $("input[id^='item_Cant']").text();
But don't works, I have not idea why, even first row. 但是不行,我也不知道为什么,即使是第一行。
But in this line I need the correct Jquery for get the last td input value in the same tr. 但是在这一行中,我需要正确的Jquery来获取同一tr中的最后一个td输入值。
I tried many lines but without success, hope you can understand me. 我尝试了很多行,但没有成功,希望您能理解我。
Thanks for your help. 谢谢你的帮助。
You are always referring to item_Cant
ID every time you do your calculation. 每次进行计算时,您总是引用item_Cant
ID。 You need to find the quantity element with respect to your current input field. 您需要找到关于当前输入字段的数量元素。
Here's one approach that'll work with your current structure: 这是一种适用于您当前结构的方法:
$(document).ready(function () {
$("input[id^='unitval']").keyup(function () {
var input_value = parseFloat($(this).val());
var Cant = $(this).closest('tr').find('[id^="item_Cant"]').text();
var totval = (input_value * Cant);
if (!isNaN(input_value)) { // the input is a number
//$("#totval1").val(totval); // update second field
$(this).closest('td').next().find('input').val(totval);
} else { // the input wasn't a number
$("#totval1").val("not a number?"); // show an error mesage
}
});
});
Here's a fiddle: http://fiddle.jshell.net/P89Tj/1/ 这是一个小提琴: http : //fiddle.jshell.net/P89Tj/1/
You cannot have multiple identical ids on a page. 一个页面上不能有多个相同的ID。
Use classes instead: 改用类:
$(document).ready(function(){
$( "input.unitval" ).keyup(function() {
var input_value = parseFloat($(this).val());
var Cant = $(this).parent().prev().text();
var totval = (input_value * Cant);
if (!isNaN(input_value)) { // the input is a number
//$("#totval1").val(totval); // update second field
$(this).parent().parent().find('.totval').val(totval);
} else { // the input wasn't a number
$("#totval1").val("not a number?"); // show an error mesage
}
});
});
<table width="500" border="1">
<tr>
<td>Quantity</td>
<td>VAL1</td>
<td>RESULT Quantity*VAL1</td>
<td>VAL2</td>
<td>RESULT Quantity*VAL2</td>
</tr>
<tr>
<td>5</td>
<td><input class="unitval" type="text"/></td>
<td><input readonly class="totval" type="text"/></td>
<td><input class="unitval" type="text"/></td>
<td><input readonly class="totval" type="text"/></td>
</tr>
<tr>
<td>4</td>
<td><input class="unitval" type="text"/></td>
<td><input readonly class="totval" type="text"/></td>
<td><input class="unitval" type="text"/></td>
<td><input readonly class="totval" type="text"/></td>
</tr>
</table>
http://fiddle.jshell.net/8CGXf/ http://fiddle.jshell.net/8CGXf/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.