jQuery 在没有提交按钮的情况下提交 html 表单?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7704976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Submit a html form without having a submit button?
提问by Richard Rodriguez
Can I submit a html <form>
with <div>
instead of <input type="submit">
?
我可以<form>
用<div>
代替提交 html<input type="submit">
吗?
Like this:
像这样:
<form method="post" action="" id="myForm">
<textarea name="reply">text</textarea>
</form>
<div>Submit the form by clicking this</div>
回答by japrescott
Quite simple;
非常简单;
document.getElementById("myForm").submit();
And if you want to do it jQuery style (which I do not recommend for such a simple task);
如果你想做 jQuery 风格(我不推荐这样一个简单的任务);
$("#myForm").submit();
回答by Peter Olson
Yes, it's fairly simple. Just use the submit
[jQuery docs]method inside of a click
handler function.
是的,这相当简单。只需在处理程序函数中使用submit
[jQuery docs]方法click
。
$("#myDiv").click(function() {
$("#myForm").submit();
});
If you prefer, you can do it with vanilla Javascript:
如果您愿意,可以使用 vanilla Javascript 来完成:
document.getElementById("myDiv").onclick = function() {
document.getElementById("myForm").submit();
};
回答by Sparky
$('#myDiv').click(function() {
$('#myForm').submit();
});
回答by Dvir Azulay
Bind the div click event.
绑定div点击事件。
$("div").click(function(){ $("form#myForm").submit(); });
$("div").click(function(){ $("form#myForm").submit(); });
回答by Samich
Using jquery:
使用jQuery:
$('#myForm').submit();
回答by Quentin
If you want to unnecessarily depend on JavaScript, then you could…
如果你想不必要地依赖 JavaScript,那么你可以……
jQuery('div').click(function () { jQuery('form').submit(); });
… however, you should use semantic HTML that works without JS being present. So use a real submit button and apply CSS to make it look the way you want.
……但是,您应该使用语义 HTML,无需 JS 即可工作。因此,使用真正的提交按钮并应用 CSS 使其看起来像您想要的那样。
回答by vzwick
For better semantics and graceful degradation, I suggest you do this:
为了更好的语义和优雅的降级,我建议你这样做:
$(document).ready(function(){
$('form input[type="submit"]').replaceWith('<div class="submit">Submit the form by clicking this</div>'); // replace submit button with div
$('.submit').live('click', function() {
$(this).closest('form').submit();
});
});
This way, your form remains usable for clients without JS.
这样,您的表单仍可用于没有 JS 的客户端。
That being said: Just style your input to look the way you want it to with CSS ;)
话虽如此:只需将您的输入样式设置为您希望使用 CSS 的方式;)
回答by Andy
why don't you just style a button to look like a regular div? :)
你为什么不设计一个按钮,让它看起来像一个普通的 div?:)
button{
border:none;
background:none;
padding:0;
text-align:left;
}
回答by user2956239
<?php
if(isset($_POST['text']) && !empty($_POST['text']))
{
echo $text_val = $_POST['text'];
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="Enter your action">
<p><label>Enter your Text : </label>
<input type="text" name="text" id="text" onmouseover="this.form.submit();"></input>
</p>
</form>
</body>
</html>
回答by Piotr Perak
How about this:
这个怎么样:
$(document).ready(function() {
$('#submitDiv').click(function() {
$('#myForm').submit();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post" action="" id="myForm">
<textarea name="reply">text</textarea>
</form>
<div id="submitDiv">Submit the form by clicking this</div>