开发者

Javascript anchor href change and form post

开发者 https://www.devze.com 2023-02-08 21:55 出处:网络
I have a form coded like below. Basically the href value for the anchor tag is determined by the value selected in a dropdown.

I have a form coded like below. Basically the href value for the anchor tag is determined by the value selected in a dropdown.

<form name=xyz method=post>
  //random stuff

  <a href="#" onclick="setAnchor()">
    //some image
  </a>
</form>

I am setting the href property of the form in javascript like this:

if(dropdown.option selected = "x")
    windows.location.href = some URL;
else
    windows.location.href = another URL;

The problem I am having is that using windows.location causes the form to use the GET method. The code expects a POST method to continue.

How can I fix this? I am not married to 开发者_StackOverflowthe above method. Any way I can set the form action URL, based on the option selected by the user, would work for me.


If you have this form:

<form id="myForm" action="page.php" method="POST">

You can submit it (with a POST method) (that is, instead of window.location...) like so:

document.getElementById("myForm").submit();

You can also set the URL to be submited like this

document.getElementById("myForm").action = "page.php";


You should submit your form:

document.xyz.submit();


document.xyz.action=page1.php; (or page2.php based on dropdown selection)
document.xyz.submit();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号