jQuery form submitting in firefox

2019-04-10 00:07发布

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?

5条回答
唯我独甜
2楼-- · 2019-04-10 00:33

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.

查看更多
Summer. ? 凉城
3楼-- · 2019-04-10 00:39

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();
查看更多
虎瘦雄心在
4楼-- · 2019-04-10 00:44

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>
查看更多
爷、活的狠高调
5楼-- · 2019-04-10 00:52

According to this , manual submit with jQuery doesn't work under Firefox when the form has been added trough Javascript (this means ajax parsed stuff too).

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  
   }

});
查看更多
我只想做你的唯一
6楼-- · 2019-04-10 00:56

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

<input type="submit" style="display:none;" />
查看更多
登录 后发表回答