开发者

jQuery submit not working in Opera

开发者 https://www.devze.com 2023-02-28 01:16 出处:网络
I have a simple form which can be submitted outside theelement via a small jQuery Script. <script type=\"text/javascript\">

I have a simple form which can be submitted outside the element via a small jQuery Script.

<script type="text/javascript">
    $(document).ready(function() {
        $('#mybutton').click(function(){
            $('#myform').attr('action', '/url/to/form').submit();
        });
    });
</script>

The button is just a normal link

<a href="javascript:void(0);" id="mybutton">Just a normal link</a>

The form is just a normal form

<form id="myform" action="/url/to/form">
    ....
    <input type="submit" value="Search">
</form>

Thi开发者_StackOverflow社区s works fine in IE, FF, Chrome, and Safari but not for Opera. In Opera it always redirects to my home page, not to the url of the form action. I have tried setting the action in the script (as above) and normally within the form action parameter but with no luck. I'm running that latest jQuery and Opera.

Please help

Edit: I'm also using the jQuery UI framework to handle some of the content and interactions


First thing I would do is change the handler to prevent the default action for the link:

    $('#mybutton').click(function(ev){
        ev.preventDefault();
        $('#myform').attr('action', '/url/to/form').submit();
    });

Using <a> tags for buttons means that you have to be careful that the browser doesn't get confused over the semantics. I don't have a lot of experience debugging things on Opera, but that's the first thing I'd suspect.

If there's no real "href" associated with the "button", I would question why it's an <a> tag at all; it could be just a simple <span>, which can also have a click handler attached but which does not have any potentially-problematic native behavior.


I believe it will be something to do with, with the currently incomplete jquery ui select class. It adds an a tag with a href as # i believe this may be your problem.


Try just $('#myform').submit(); instead of $('#myform').attr('action', '/url/to/form').submit();

Edit: As the other answer said, you will first have to call event.preventDefault(); before doing anything on the click event, as that prevents the browser from redirecting you.

Edit2: The right code would be:

<script type="text/javascript">
$(document).ready(function() {
    $('#mybutton').click(function(event){
        event.preventDefault();
        $('#myform').attr('action', '/url/to/form').submit();
    });
});

This should work fine.

0

精彩评论

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

关注公众号