简体   繁体   中英

Pass an extra param on form submit in javascript

I have a javascript function say newFn() . I need to check some condition with if else .

function newFn(formName)
{
 var codeParam = document.getElementById('code').value; 
 var windowForm = document.forms[formName];     
   windowForm.submit(); 
 return true;           
}

My requirement is :

if(codeParam == null) I need to pass one more parameter say codeParam=null else codeParam=codeParam on windowForm.submit(); .In short in the url I need an extra param. Is this possible?

Why not directly set its value like this:

if (codeParam == null) {
    document.getElementById('code').value = 'null';
}

This will send a codeParam=null form data to your action url.

However I noticed that this code would never be executed, the value property of a input element could never be the value null , when no input is provided, the value property's value is a empty string ('') , so maybe you should change your logic.

I don't understand exactly what you want to accomplish but

function newFn(formName) {
    var codeParam = document.getElementById('code').value;
    if (!codeParam) {
        codeParam = null;
    }
    document.forms[formName].codeParam = codeParam;
    var windowForm = document.forms[formName];     
    windowForm.submit(); 
    return true;
}

If (assuming "code" is a textfield) is empty, codeParam is set to null, if it has a value, then codeParam is set to the value. (i added the propertie to the form as well, so it would get submitted, i don't know if its what you want )

You can take a look at the output of this Fiddle

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