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

javascript - jQuery form submitting in firefox - Stack Overflow

programmeradmin0浏览0评论

Please, help me with one problem. I have this code, for submitting form via anchor.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".dtd">
<html xmlns="">
<head>
    <title></title>
    <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#btnLogout").click(function() {
                $('#frm').submit();
                return false;
            });
        });
    </script>
</head>
<body>
    <form id="frm" action="/" method="post">
        <div>
            <p>
                <label for="txtLogin">Login:</label>
                <input name="txtLogin" />
            </p>
        <div>
           <a id="btnLogout" href="javascript:void(0)">выход</a>  
        </div>
        </div>
    </form>
</body>
</html>

It works fine on IE7,8, Opera and Google Chrome, but does not work on FireFox 3.5. I can not understand why it does not work?

Please, help me with one problem. I have this code, for submitting form via anchor.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="js/jquery-1.3.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#btnLogout").click(function() {
                $('#frm').submit();
                return false;
            });
        });
    </script>
</head>
<body>
    <form id="frm" action="/" method="post">
        <div>
            <p>
                <label for="txtLogin">Login:</label>
                <input name="txtLogin" />
            </p>
        <div>
           <a id="btnLogout" href="javascript:void(0)">выход</a>  
        </div>
        </div>
    </form>
</body>
</html>

It works fine on IE7,8, Opera and Google Chrome, but does not work on FireFox 3.5. I can not understand why it does not work?

Share Improve this question edited Mar 28, 2012 at 16:07 Teun Zengerink 4,3935 gold badges32 silver badges32 bronze badges asked Feb 23, 2010 at 15:19 msimsi 3,2124 gold badges28 silver badges37 bronze badges 11
  • @msi <a id="btnLogout" href="javascript:;">выход</a> – ant Commented Feb 23, 2010 at 15:21
  • What exactly doesn't work? Your form does not have an action and nothing is done in the submit function so it shouldn't do anything. – ryanulit Commented Feb 23, 2010 at 15:29
  • I want to use <a id="btnLogout"... for sign out from the site. I'm using ASP.NET MVC, so I want form has an action of controller. So correct action will be <form id="frm" action="/Security/Logout" method="post"> and it should not send any data to server. – msi Commented Feb 23, 2010 at 15:34
  • @msi I wonder how can form work without sending any data to the server .. – ant Commented Feb 23, 2010 at 15:36
  • @c0mrade if you have not any inputs on your form, it would not send any data to server. – msi Commented Feb 23, 2010 at 15:47
 |  Show 6 more comments

5 Answers 5

Reset to default 18

Based on the answer to the same question here: Jquery Form.submit() on Chrome works but not in Firefox

Add the form object to the DOM before submitting:

$("#actionform").appendTo("body").submit();

According to this , manual submit with jQuery doesn't work under Firefox when the form has been added trough Javascript

The solution consists to clone the form and submit it :

$('#myform').on('submit', function(e) {
   e.preventDefault();
   if(navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
     $(this).clone().appendTo("body").submit(); // FF only
   } else {
     this.submit(); // works under IE and Chrome, but not FF  
   }

});

This works for me :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script>

    <script type="text/javascript">
  $(document).ready(function() {
    $("#btnLogout").click(function() {
      $("#actionform").submit();
         return false;
    });
  });
  </script>

</head>

<body>

<form id="actionform" action="something.html" method="post" name="forma">

        <label for="txtLogin">Login:</label>
        <input name="txtLogin" />
     <a href="#" id="btnLogout">Uno mas</a>  

</form>

</body>
</html>

Try to include a submit button in your form. Even if it is hidden.

<input type="submit" style="display:none;" />

This may be a FF issue not related to jQuery directly. Try putting a filename in the action attribute like this:

<form id="frm" action="/index.html" method="post">

Just make sure to change index.html to whatever your default document is.

发布评论

评论列表(0)

  1. 暂无评论