[removed].href not working in form onsubmit

后端 未结 4 1081
悲&欢浪女
悲&欢浪女 2020-12-08 23:35

So i have a form, and onsubmit=\"return reg_check(this)\" where reg_check() is a javascript function in the header which is supposed to check the f

相关标签:
4条回答
  • 2020-12-08 23:59

    You need to return false; from your reg_check function and then in your onsubmit, change it to:

    onsubmit="return reg_check(this);"
    

    This will cancel the form submission. And if you want to let the form submit as normal, just return true from reg_check.

    Edit (to be more clear you need to add return false; from your function):

    function reg_check(myForm) {
        alert("before redirect..");
        window.location.href = "http://localhost/main.php?width=" + screen.width + "&height=" + screen.height;
        return false;
    }
    
    0 讨论(0)
  • 2020-12-09 00:00

    [RESOLVED] I had a similar problem in redirecting to some other url from client script. Implemented window.open function instead and it worked. You may have for instance, a function say ChangeCity() for your html control event that gets called with onchange event.

    function ChangeCity() {
        switch ($("#currentCity").val()) {
            case "NY":
                  var url = '@Url.Action("New York City", "Home", new { @area = "" },Request.Url.Scheme)';
                window.location.href = url;
                window.open(url,"_top");
                return false;
    

    /* cases for other cities */

        }
    

    You may like to explore details on window.location.href redirection -Alternative Solution

    0 讨论(0)
  • 2020-12-09 00:03

    I've seen problems in IE where I've had to do the following:

    window.location.assign(url);
    

    Specifically within a jQuery AJAX success handler. Can't really speculate to why other than for me, it worked.

    Actually I did

    window.location.replace(url)
    

    Which replaces the current page in the history. Nice trick that!

    0 讨论(0)
  • 2020-12-09 00:07

    I had the same problem, but found the answer here Javascript: window.location.href doesn't redirect when calling function within a function . My button was reloading the page by default so if you add an

     event.PreventDefault();
    

    To your function it should work.

    0 讨论(0)
提交回复
热议问题