简体   繁体   中英

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. In short, there is a button that calls a JS function. 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. 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:

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:

function remove(salNum) {

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

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

    document.forms[0].submit();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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