jquery 对动态生成的元素执行计算
我有一个由一行 3 个输入元素组成的表:价格、数量和总计。在其下,我有两个链接,可以单击以动态生成表中的另一行。所有这些都运行良好,但实际计算总元素的值给我带来了麻烦。我知道如何计算第一个总元素的值,但是当我向表中添加新行时,我在扩展此功能时遇到了麻烦。这是 html:
<table id="table" border="0">
<thead>
<th>Price</th><th>Quantity</th><th>Total</th>
</thead>
<tbody>
<tr>
<td><input id ="price" type = "text"></input></td>
<td><input id ="quantity" type = "text"></input></td>
<td><input id ="total" type = "text"></input></td>
</tr>
</tbody>
</table>
<a href="#" id="add">Add New</a>
<a href="#" id="remove">Remove</a>
这是我正在使用的 jquery:
$(function(){
$('a#add').click(function(){
$('#table > tbody').append('<tr><td><input id ="price" type = "text"></input></td><td><input id ="quantity" type = "text"></input></td><td><input id ="total" type = "text"></input></td></tr>');
});
$('a#remove').click(function(){
$('#table > tbody > tr:last').remove();
});
});
$(function(){
$('a#calc').click(function(){
var q = $('input#quantity').val();
var p = $('input#price').val();
var tot = (q*p);
$('input#total').val(tot);
});
});
我是 jquery 的新手,所以可能有一个我不知道的简单方法来选择我想要计算的相关字段。任何指导将不胜感激。
I have a table made up of a row of 3 input elements: Price, Quanity, and Total. Under that, I have two links I can click to dynamically generate another row in the table. All that is working well, but actually calculating a value for the total element is giving me trouble. I know how to calculate the value of the first total element but I'm having trouble extending this functionality when I add a new row to the table. Here's the html:
<table id="table" border="0">
<thead>
<th>Price</th><th>Quantity</th><th>Total</th>
</thead>
<tbody>
<tr>
<td><input id ="price" type = "text"></input></td>
<td><input id ="quantity" type = "text"></input></td>
<td><input id ="total" type = "text"></input></td>
</tr>
</tbody>
</table>
<a href="#" id="add">Add New</a>
<a href="#" id="remove">Remove</a>
Here's the jquery I'm using:
$(function(){
$('a#add').click(function(){
$('#table > tbody').append('<tr><td><input id ="price" type = "text"></input></td><td><input id ="quantity" type = "text"></input></td><td><input id ="total" type = "text"></input></td></tr>');
});
$('a#remove').click(function(){
$('#table > tbody > tr:last').remove();
});
});
$(function(){
$('a#calc').click(function(){
var q = $('input#quantity').val();
var p = $('input#price').val();
var tot = (q*p);
$('input#total').val(tot);
});
});
I'm new to jquery so there's probably a simple method I don't know about that selects the relevant fields I want to calculate. Any guidance would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
多个元素不能共享相同的 ID。 ID 在整个文档中必须是唯一的。
也就是说,您必须更改代码以使用类:
并且:
为了提供完整的解决方案,您的 ID calc 的链接在哪里。计算应该如何进行?每一行是否应该有自己的
计算
按钮,还是有一个全局按钮来计算每一行的值?如果是后者,您可以使用如下内容:
说明:
当您单击链接时,该函数将循环遍历表中的每一行,搜索具有
quantity
和price
并将结果放入该行中类total
的输入中。请注意,我使用
parseFloat
来确保使用数字进行计算。另请注意,您不必在创建的每一段 JS 代码中都使用
$(function(){})
。将加载 DOM 之后需要执行的所有内容(因为您从代码中访问元素)放入此函数中。所以你应该这样做:更新:如果你想使用
.blur()
方法,这里有一个例子:.closest()
获取与选择器匹配的最接近的父级(在本例中为行),其余类似到我编写的第一个函数(获取特定行的值。我使用较新的方法
delegate( )
在这里,但您也可以使用.live()$(input.total).blur(function(){})
将运行(当加载 DOM 时)尚未创建任何行。You cannot have multiple elements that share the same ID. An ID has to be unique throughout the whole document.
That said, you have to change your code to use classes:
And:
In order to provide a complete solution, where is your link with ID
calc
. How should the calculation work? Should every row has its owncalculation
button or do you have one global button the calculates the values for every row?If the latter you can use something like this:
Explanation:
When you click the link, the function loops over every row in the table, searches for the to input fields with class
quantity
andprice
and puts the result into the input with classtotal
in that row.Note that I use
parseFloat
to make sure to use numbers for the calculation.Also note that you don't have to use
$(function(){})
around every single piece of JS code you create. Put everything that needs to be executed after the DOM loaded (because you access elements from your code) into this function. So you should do:Update: If you want to use the
.blur()
method, here is an example:.closest()
gets the closest parent that matches the selector (in this case the row) and the rest is similar to the first function I wrote (getting the values of the particular row.I use the newer method
delegate()
here, but you can also use.live()
. Note that you have to use one of them because your are creating the row dynamically and at the time e.g.$(input.total).blur(function(){})
would be run (when the DOM is loaded) no row is created yet.您正在为这些新表行创建重复的“id”值。 id 在任何一个页面中应该是唯一的(它可以在多个页面上使用,但每个页面只能使用一次)。
$('input#quantity')
只会返回它找到的 id 的第一个(也可能是最后一个)实例,因为它不应该出现多次。解决方法是将所有 id 更改为
name=
,然后执行类似以下操作(在伪代码中:You're creating duplicate 'id' values for those new table rows. An id is supposedly to be unique within any one page (it can be used on multiple pages, but only once per page).
The
$('input#quantity')
will only return the first (or maybe last) instance of the id it finds, since it's not supposed to occur multiple times.A workaround would be to change all the ids to
name=
, then do something like this (in pseudo-code: