最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to get this javascript redirect to work in Firefox? - Stack Overflow

programmeradmin7浏览0评论

I have got help here to put together this code. And it works perfect in Chrome, Safari and in Internet Explorer. But in Firefox it redirects to a sub-url (probably not right word for it...)

I have the script on a page:

And I want to redirect to a new page based on the value the user chooses (and then click the button): So if I choose option #2 I want to get to here:

It works in the other browsers, but not in Firefox. In Firefox it in stead redirects to: %2Fmy-test-2 which of course doesn't lead anywhere.

The HTML is loaded in a jquery and Bootstrap environment, and I do use modals on that page. I just mention this in case there is a know error for Firefox using those framworks.

Here is the HTML:

I want to choose:
<form  id="mychoice">
    <input type="radio" name="redirect" value=""><span>1</span><br>
    <input type="radio" name="redirect" value=""><span>2</span><br>
    <input type="radio" name="redirect" value=""><span>3</span><br>
    <input type="radio" name="redirect" value=""><span>4</span><br>
    <input type="radio" name="redirect" value=""><span>5</span><br /><br />
    <input type="submit" value="See result">
</form>

The javascript:

$(document).ready(function(){    
    $("#mychoice").submit(function(){
        event.preventDefault();
        var loc = $('input[name="redirect"]:checked').val();
        window.location = loc;
        return false;    
    });
});

Here is a fiddle

Do you see the reason why Firefox behaves like this?

If of interest: I work on a Mac OSX 10.10.2 with Chrome 42.0.2311.90 (64-bit), Firefox 37.0.2 and Windows 8.1 IE 11.09600.17728

I have got help here to put together this code. And it works perfect in Chrome, Safari and in Internet Explorer. But in Firefox it redirects to a sub-url (probably not right word for it...)

I have the script on a page:

http://example./test

And I want to redirect to a new page based on the value the user chooses (and then click the button): So if I choose option #2 I want to get to here: http://example./my-test-2

It works in the other browsers, but not in Firefox. In Firefox it in stead redirects to: http://example./test?redirect=http%3A%2F%2Fexample.%2Fmy-test-2 which of course doesn't lead anywhere.

The HTML is loaded in a jquery and Bootstrap environment, and I do use modals on that page. I just mention this in case there is a know error for Firefox using those framworks.

Here is the HTML:

I want to choose:
<form  id="mychoice">
    <input type="radio" name="redirect" value="http://example./my-test-1"><span>1</span><br>
    <input type="radio" name="redirect" value="http://example./my-test-2"><span>2</span><br>
    <input type="radio" name="redirect" value="http://example./my-test-3"><span>3</span><br>
    <input type="radio" name="redirect" value="http://example./my-test-4"><span>4</span><br>
    <input type="radio" name="redirect" value="http://example./my-test-5"><span>5</span><br /><br />
    <input type="submit" value="See result">
</form>

The javascript:

$(document).ready(function(){    
    $("#mychoice").submit(function(){
        event.preventDefault();
        var loc = $('input[name="redirect"]:checked').val();
        window.location = loc;
        return false;    
    });
});

Here is a fiddle

Do you see the reason why Firefox behaves like this?

If of interest: I work on a Mac OSX 10.10.2 with Chrome 42.0.2311.90 (64-bit), Firefox 37.0.2 and Windows 8.1 IE 11.09600.17728

Share edited May 5, 2015 at 10:12 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked May 5, 2015 at 10:06 PrebenPreben 1,2872 gold badges11 silver badges20 bronze badges 1
  • Do you see the reason why Firefox behaves like this? You should really debug your code using browser console, this would be fixed really really quickly – A. Wolff Commented May 5, 2015 at 10:13
Add a ment  | 

2 Answers 2

Reset to default 12

You've used preventDefault(), but haven't actually passed the event in to the handler. Also note that using window.location.assign() is better practice. Try this:

$("#mychoice").submit(function(event) { // < 'event' is the parameter passed to the func
    event.preventDefault();
    window.location.assign($('input[name="redirect"]:checked').val());
});

you have an error un your JS. The variable event is not defined. Try it like that :

$(document).ready(function(){
    $("#mychoice").submit(function(event){
        event.preventDefault();
        var loc = $('input[name="redirect"]:checked').val();
        window.location.href = loc;
        return false;    
    });
});
发布评论

评论列表(0)

  1. 暂无评论