[英]How to select a input field in next TD with Jquery?
I want to select a input field in next TD with Jquery. 我想用Jquery在下一个TD中选择一个输入字段。 I can not use ID or class because the table's row can be add or removed.
我不能使用ID或类,因为可以添加或删除表的行。
like , 喜欢 ,
<tr>
<td><input type='text' name='price[]' onkeyup = 'getTest(this)' /> </td>
<td><input type='text' name='qty[]' onkeyup = 'getPrice(this)' value='1' /> </td>
<td><input type='text' name='amount[]' /> </td>
</tr>
function getTest(obj){
//I want to select qty[] and amount[]
}
function getPrice(obj){
//I want to select price[] and amount[]
}
anybody know how to select the input fields, please help~! 任何人都知道如何选择输入字段,请帮忙〜!
Thanks 谢谢
您的网页预期行为的working demo
。
Chinmayee's answer is much better than this one, but if you don't want to change any of your HTML mark-up, you can simply drop this into your JavaScript and it will work: Chinmayee的答案比这个答案要好得多,但是如果您不想更改任何HTML标记,只需将其放入JavaScript中就可以了:
function getTest(obj) {
var tr = $(obj).closest('tr');
var qty = tr.find('input[name="qty[]"]');
var amount = tr.find('input[name="amount[]"]');
console.log('qty: ' + qty.val());
console.log('amount: ' + amount.val());
}
function getPrice(obj) {
var tr = $(obj).closest('tr');
var price = tr.find('input[name="price[]"]');
var amount = tr.find('input[name="amount[]"]');
console.log('price: ' + price.val());
console.log('amount: ' + amount.val());
}
But the reason why Chinmayee's answer is better than mine is because it uses unobtrusive JavaScript, and it also uses event delegation, so it will perform better. 但是之所以Chinmayee的答案比我的要好,是因为它使用了不引人注目的JavaScript,并且还使用了事件委托,因此它的性能会更好。
Here's a possible solution for your problem (assuming you want amount to be price * qty ): 这是您问题的一种可能解决方案(假设您希望金额为price * qty ):
$(document).ready(function(){
$('[name="price[]"], [name="qty[]"]').live('keyup', function(){
var inputs = $(this).closest('tr').find('input');
$(inputs[2]).val($(inputs[0]).val()*$(inputs[1]).val());
});
});
Your getTest() and getPrice() methods are no longer required with this solution and you can also drop the onkeyup="..."
attributes on the input
elements. 此解决方案不再需要您的getTest()和getPrice()方法,您还可以在
input
元素上删除onkeyup="..."
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.