简体   繁体   English

在jquery中删除动态表单字段时出现问题

[英]Problem removing a dynamic form field in jquery

I'm trying to remove a dynamic form field by click a button. 我正在尝试通过单击按钮来删除动态表单字段。 It will also subtract whatever values it had from the total amount from my calculation. 它也会从我的计算中减去总额中的任何值。 This is the code: 这是代码:

function removeFormField(id) {
    var id = $(id).attr("name");
    $('#target1').text($("#total" + id).map(function() {
        var currentValue = parseFloat(document.getElementById("currentTotal").value);
        var newValue = parseFloat($("#total" + id).text());
        var newTotal = currentValue - newValue;
        document.getElementById("currentTotal").value = newTotal;
        return newTotal;
        }).get().join());
    $(id).remove();
}

Okay, it will do the subtraction portion of the code with no problem, this issue is with the last line to remove the field. 好的,它将毫无问题地执行代码的减法部分,此问题在于删除字段的最后一行。 If I comment out the rest of the code it will work, but not with the rest of the code. 如果我注释掉其余的代码,它将起作用,但不能与其余的代码一起工作。 I know this is something simple, yet I can't seem to wrap my mind around it. 我知道这很简单,但我似乎无法全神贯注。 Can someone please help? 有人可以帮忙吗?

You're setting id to equal the name of the form element with: 您正在将id设置为等于表单元素的名称,并具有:

var id = $(id).attr("name");

Then trying to get it with: $(id) at the end. 然后尝试通过以下方式获取它: $(id) Try changing the last line to actually use the ID of the element you are trying to delete - remember the '#' before it. 尝试更改最后一行以实际使用您要删除的元素的ID-请记住它前面的“#”。 Without seeing what is passed into the removeFormField() as the id parameter I can't be sure what you need to change. 如果没有看到将什么作为id参数传递给removeFormField() ,我不能确定需要更改什么。

The important bit to remember is form name attributes are not the same as element IDs. 要记住的重要一点是表单name属性与元素ID不同。

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

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