简体   繁体   English

克隆没有任何值的最后一个表行

[英]Clone last table row without any value

I am working on a update feature.我正在开发更新功能。 In this page I have a table with multiple textfields.在此页面中,我有一个包含多个文本字段的表格。 An example like像这样的一个例子

<input class="form-control" type="text" name="derivable[]" value="2">

On button click I need to add a new row.单击按钮时,我需要添加一个新行。

$("#main-table").each(function () {
    let tds = '<tr>';
    jQuery.each($('tr:last td', this), function () {
            tds += '<td class="text-center">' + $(this).html() + '</td>';

It is adding a new row but $(this).html() is also copying value = 2 from last row.它正在添加一个新行,但$(this).html()也在从最后一行复制 value = 2 。

Is it possible to get $(this).html() without value?是否有可能获得没有价值的$(this).html()

I don't think that is directly possible.我不认为这是直接可能的。 You should inspect each element / property of $(this) to build the new object.您应该检查$(this)的每个元素/属性以构建新的 object。

I will suggest copy the complete object as you are doing and remove the value later.我建议您复制完整的 object 并稍后删除该值。

Something like (not tested code):类似(未测试的代码):

$("#main-table").each(function () {
    let tds = '<tr>';
    jQuery.each($('tr:last td', this), function () {
            tds += '<td class="text-center">' + $(this).html() + '</td>';
            [ ... ]   
    }); 
    jQuery.each($('tr:last td', this), function () {
        $(this).removeAttr('value');
    });

But i will also think if the copy process is need at all.但我也会考虑是否需要复制过程。 If your inputs are always the same, why not hardcode how they are and avoid the copy.如果你的输入总是相同的,为什么不硬编码它们的样子并避免复制。 Just insert.插入即可。

And also, I usually prefer not to work with the html representation but with the jQuery objects, where you can use clone to get a new instance.而且,我通常不喜欢使用 html 表示,而是使用 jQuery 对象,您可以在其中使用克隆来获取新实例。 Having the instance you call removeAttr on it, without having to use regular expressions that are more difficult to debug.拥有您调用removeAttr的实例,而不必使用更难调试的正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM