admin管理员组

文章数量:1332107

I am trying to redirect my page to a different url depending on what the user selects from the drop down list. If the user selects "A", the url is the default URL that's in the action attribute of the form but if the user selects "B", I want to redirect it to a different url.

My problem has been no matter what I do, it always redirects to the default URL(ie oldUrl.do) even if I select "B" from the drop down.

What can I do to point it to the newUrl.do?

Note: I am using Struts 1.2 and testing it on IE8.

My jsp page:

<form name="myForm" method="post" action="oldUrl.do" id="myForm">

    <select name="modeOfT" id="choice"><option value="A">A</option>
        <option value="A">A</option>
        <option value="B">B</option>
    </select> 

    <input type="submit" name="submitBtn" onclick="submitForm()">
</form>

Javascript function:

function submitForm(){
    var form = document.getElementById("myForm");
    var mode = document.getElementById("choice");
    if(mode.value == "B"){
        $(form).attr("action", "newUrl.do");
    }
} 

Following are other things I have tried and they still take me to "oldUrl.do":

  1. document.location.href("newUrl.do");

  2. window.location.href="newUrl.do";

  3. top.location.href="newUrl.do";

  4. parent.location.href="newUrl.do";

  5. window.location.replace("newUrl.do");

  6. window.location="newUrl.do";

I am trying to redirect my page to a different url depending on what the user selects from the drop down list. If the user selects "A", the url is the default URL that's in the action attribute of the form but if the user selects "B", I want to redirect it to a different url.

My problem has been no matter what I do, it always redirects to the default URL(ie oldUrl.do) even if I select "B" from the drop down.

What can I do to point it to the newUrl.do?

Note: I am using Struts 1.2 and testing it on IE8.

My jsp page:

<form name="myForm" method="post" action="oldUrl.do" id="myForm">

    <select name="modeOfT" id="choice"><option value="A">A</option>
        <option value="A">A</option>
        <option value="B">B</option>
    </select> 

    <input type="submit" name="submitBtn" onclick="submitForm()">
</form>

Javascript function:

function submitForm(){
    var form = document.getElementById("myForm");
    var mode = document.getElementById("choice");
    if(mode.value == "B"){
        $(form).attr("action", "newUrl.do");
    }
} 

Following are other things I have tried and they still take me to "oldUrl.do":

  1. document.location.href("newUrl.do");

  2. window.location.href="newUrl.do";

  3. top.location.href="newUrl.do";

  4. parent.location.href="newUrl.do";

  5. window.location.replace("newUrl.do");

  6. window.location="newUrl.do";

Share Improve this question edited Feb 12, 2013 at 17:45 Susie asked Feb 12, 2013 at 17:39 SusieSusie 5,15811 gold badges55 silver badges74 bronze badges 4
  • 3 Have you tried window.location = "your URL"; – Will Hawker Commented Feb 12, 2013 at 17:42
  • yes i have. Thank you. I will add that to my list. – Susie Commented Feb 12, 2013 at 17:43
  • 1 Your code seems to work fine jsfiddle/xv3Uf/5 (I still suggest using the submit handler as in my answer) – Ruan Mendes Commented Feb 12, 2013 at 18:11
  • @javaStudent Please provide some feedback on what we've told you. I'm curious to find out what wasn't working... – Ruan Mendes Commented Feb 12, 2013 at 18:59
Add a ment  | 

3 Answers 3

Reset to default 4

You should not handle the click event. To deal with a form being submitted you should use the submit event, for this and many other reasons (people can use enter, they can hit space while focus in on the submit button)

Working example http://jsfiddle/xv3Uf/2/

HTML

<form name="myForm" method="post" action="oldUrl.do" id="myForm" onsubmit="return submitForm()" >

JS

function submitForm(){
    var form = document.getElementById("myForm");
    var mode = document.getElementById("choice");
    if(mode.value == "B"){
        form.action = "newUrl.do";
    }
} 

And since you're already using jQuery, why not drink the kool-aid and stop the pre-enlightenment practice of setting JS handlers in the HTML? http://jsfiddle/xv3Uf/3/

<form name="myForm" method="post" action="oldUrl.do" id="myForm">

JS

$('#myForm').submit(function(e){
    if ( $('#choice').val() === 'B') {
        this.action = 'newUrl.do'
    }
});

You could:

  1. Have the server send back an HTTP/redirect header depending on what the user selects in the form.

  2. If you want the redirect to be handled at the browser, use an "onsubmit" attribute, and return a false value. You can handle the redirection within this function using window.location = <URL>;

    <form onsubmit="return myJSFunc()" .. >
       ...
       <input type="submit">
    </form>
    

Turn the <submit> into a regular <button>.

In the submitForm() function, at the end, call:

document.forms["myForm"].submit();

本文标签: javaRedirecting to a new URL using javascriptStack Overflow