如何通过点击元素来提交表单?
我尝试了以下代码,但Chrome使用http GET方法发送代码。如何解决这个问题,以便使用POST方法发送表单?
<html>
<body>
<form id="_form" method='post' target='_blank'>
<input type="email" value="me@company.com" name="_email" />
<a href='SendEMail?_entity=DokGReport&_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>
答案 0 :(得分:2)
您需要阻止点击锚点的默认操作,即链接:
$(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();
});
});