简体   繁体   中英

Passing URL parameter with JavaScript

I have some JavaScript that creates Forward and Back buttons. However, I need to pass a parameter in the URL ( ?id=$idd ):

<a href="javascript:submitForm('mainForm','back');" title="Go back to the kit home page" style="float: left;"><img src="images/back.gif" alt="Go back to the kit home page" border="0" /></a>
<a href="javascript:submitForm('mainForm','proceed');" title="Submit the order details" style="float: right;"><img src="images/proceed.gif" alt="Proceed to the next page" border="0" /></a>

The JavaScript is below:

// Used in all pages to submit a form and optionally set a hidden 
// form varaible called 'navigate' to direct navgiation
function submitForm(formName, navigateValue) {
    if (navigateValue != null && navigateValue != "") {
        document.forms[formName].navigate.value = navigateValue;
    }
    document.forms[formName].submit();
}

Thanks.

您需要将变量添加到<form>指向的url中:

<form action="example.com?id=14">

I'm guessing at what you're asking, and it doesn't make much sense because if you're submitting a form, why wouldn't you put the data in the form, but it sounds like you just need to modify the forms' action property. Something like:

document.forms[formName].action += "?id=$idd";

But this depends strongly on the existing action value and you will probably want to sanitise that with a validating method.

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