为什么 HTML 表的最后一个值行值会重复
我有一个静态 Html 表,其中包含一个“添加”按钮,当单击该按钮时,它会创建一个新行,其中包含一些文本框 并将其追加到此表中。但我的问题是我有 3 行,当我单击“添加”时,我们可以创建新的 ROW,但上一行的值 被复制到正在生成的新行中。这是我正在使用的函数,
$('#btn1').click(function(){
tr= $("#Tbl tr:last").html();
num= $('#num').val()*1;
first= $("#Tbl tr:last").attr('id').substr(4)*1;
last=first+num;
for(i=first+1;i<=last;i++) {
var newtr= tr.replace(first,i);
$('#Tbl').append('<tr id="txt_'+i+'">'+newtr+'</tr>');
}
});
当添加新行时,我如何在选择框中将值选择为“ABC”。我正在使用此语句,但没有锻炼。如何我可以将值设置为所选值吗
$('#Tbl tr:last :input').find('input[name="code"]').val('ABC');
I have a static Html Table which contains one button "Add" and when clicking on that button it creates a new row which contains some text boxes
and it append to this table .But my Problem is say i have 3 rows and when i click "Add" we are able to create the new ROW but the Previous row values
are copied in to new row that is being generated .Here is the Function That iam using
$('#btn1').click(function(){
tr= $("#Tbl tr:last").html();
num= $('#num').val()*1;
first= $("#Tbl tr:last").attr('id').substr(4)*1;
last=first+num;
for(i=first+1;i<=last;i++) {
var newtr= tr.replace(first,i);
$('#Tbl').append('<tr id="txt_'+i+'">'+newtr+'</tr>');
}
});
How do i make the value Selected as "ABC" in Select box when the new Row is being added.Iam using this statement But did not workout .How can i make the value as selected
$('#Tbl tr:last :input').find('input[name="code"]').val('ABC');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不是真正的 jQuery 英雄,但为什么不简单地使用这个呢?
I'm not a real jQuery hero, but why not use simply this?
在我看来,当您
从上一行中抓取所有 html(包括该行中任何控件的值)并将其粘贴到新行中时
it looks to me that when you do
your grabbing all of the html from inside that previous row (including the values of any controls inside that row) and pasting it into the new row
当您在文本框中键入内容时,它会设置 HTML 中的“value”属性。因此,稍后,当您复制前一行的innerHTML 时,它会采用这些“值”属性并将它们复制到新行中。
您可能希望将数据保留在脚本中并使用它来生成新行,而不是解析上一行的 HTML。
知道我的意思吗,果冻豆?
When you type into a text box, it sets the "value" attribute in the HTML. So later, when you copy the innerHTML of the previous row, it takes those "value" attributes and duplicates them in your new row.
Instead of parsing the HTML of the previous row, you might want want to keep your data within your script and use that to generate new rows.
Know what I mean, jelly bean?
试试这个:
Try this:
正如@Patrick 指出的,您可能会复制最后一行的值。那么为什么不在创建新行后清除它们呢?在 for 循环之后执行此操作:
这应该选择 #Tbl 表的最后一个 tr 中的输入字段并从中删除值。这将专门选择最后一行,但现在我看到您添加了可变数量的行,所以让我们正确处理它。
既然您要附加一堆行,为什么不在添加时清理它们呢?
这应该处理您可以添加的尽可能多的行。
As @Patrick points out, you may be copying in values from the last row. So why not clear them out after you create the new row? Do this after the for loop:
This should select the input fields in the last tr of the #Tbl table and remove values from them. This will specifically select the last row, but now I see that you're adding a variable number of rows, so lets handle that properly.
Since you're appending a bunch of rows, why not clean them up as you add them?
That should handle as many rows as you can add.