提交HTML表单而没有提交按钮? [英] Submit an HTML form without having a submit button?
本文介绍了提交HTML表单而没有提交按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以用<div>
而不是<input type="submit">
提交html <form>
吗?
Can I submit a html <form>
with <div>
instead of <input type="submit">
?
赞:
<form method="post" action="" id="myForm">
<textarea name="reply">text</textarea>
</form>
<div>Submit the form by clicking this</div>
推荐答案
可用于提交特定表单的方法如下:
The method you can use to submit a specific form is the following:
// Grab the form element and manually trigger the 'submit' method on it:
document.getElementById("myForm").submit();
因此,在您的示例中,您可以在喜欢的任何元素上添加click事件处理程序,并通过该事件触发表单的Submit方法:
So in your example, you can add a click event handler on any element you like and trigger the form's submit method through that:
<form method="post" id="myForm">
<textarea name="reply">text</textarea>
</form>
<div class="submit">Submit the form by clicking this</div>
const myForm = document.getElementById("myForm");
document.querySelector(".submit").addEventListener("click", function(){
myForm.submit();
});
如果您想使用jQuery样式(对于这种简单的任务,我不推荐使用 );
And if you want to do it jQuery style (which I do not recommend for such a simple task);
$("#myForm").submit();
完整形式:
const myForm = $("#myForm");
$(".submit").click(function(){
myForm.submit();
});
参考:
- The submit() method of HTML Form elements (native
JavaScript API
) - The
jQuery
submit() API
这篇关于提交HTML表单而没有提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文