How to post form by clicking in anchor element

2019-09-02 02:17发布

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条回答
贼婆χ
2楼-- · 2019-09-02 02:50

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();
          });

    });
查看更多
登录 后发表回答