简体   繁体   English

删除动态添加的表单字段

[英]Removing dynamically added form fields

OK, I'm out of ideas here. 好的,我这里没主意。 I have some JavaScript that was apparently written by someone a while ago. 我有一些JavaScript显然是某人以前写的。 In short, there is a button that calls a JS function. 简而言之,有一个调用JS函数的按钮。 The function is to remove form fields that were added just prior. 该功能是删除之前添加的表单字段。 It works in IE 8 and older, but not in IE9 or FireFox. 它在IE 8及更早版本的IE中有效,但在IE9或FireFox中则无效。 The function is: 该函数是:

function remove(salNum) {

    var fld
    fld = document.getElementById('salary' + salNum);
    fld.value = 0;

    document.forms[0].submit();

}

The output in the FireBug console gives and error: FireBug控制台中的输出给出并显示错误:

fld is null
fld.value = 0;

What am I missing? 我想念什么?

try this in case the element you are trying to remove has already been removed or the html has been altered: 如果您要删除的元素已被删除或html被更改,请尝试以下方法:

function remove(salNum) {

    var fld
    fld = document.getElementById('salary' + salNum);

// check for element existance
if( fld ) fld.value = 0;

    document.forms[0].submit();

}

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

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