How to post form by clicking in anchor element

谁说胖子不能爱 提交于 2019-12-13 05:31:34

问题


How to submit form by clicking in a element ?

I tried code below but Chrome sends it using http GET method. How to fix this so that form is sent using POST method ?

<html>
<body>
    <form id="_form" method='post' target='_blank'>
        <input type="email" value="me@company.com" name="_email" />
        <a href='SendEMail?_entity=DokGReport&amp;_dokumnrs=135361'
           id='sendemail' class='button'>
            Send
        </a>
    </form>

    <script>
        $(function () {
            $('#sendemail').button({ icons: { primary: "ui-icon-mail-closed" } })
              .click(function () {
                  $('#_form')[0].action = $(this).attr('href');
                  $('#_form')[0].submit();
              });

        });
    </script>
</body>
</html>

回答1:


You need to prevent the default action of clicking on the anchor, which is to follow the link:

    $(function () {
        $('#sendemail').button({ icons: { primary: "ui-icon-mail-closed" } })
          .click(function (e) {
              e.preventDefault(); // Don't follow the link normally
              $('#_form').attr('action', $(this).attr('href')).submit();
          });

    });


来源:https://stackoverflow.com/questions/20767094/how-to-post-form-by-clicking-in-anchor-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!