php 按“输入”键提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29046077/
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 form on 'enter' key press
提问by user3386779
I want to submit the form value in 'enter' key.I have a button search now to submit the value value.but I want to do it without search button on enter key press.
我想在“输入”键中提交表单值。我现在有一个按钮搜索来提交值值。但我想在没有按回车键的搜索按钮的情况下执行此操作。
html
<input class="stxt" type="text" name="searchtxt"><a style="padding-right:2ex;"></a>
<button name="search" class="search">Search</button></p>
php
if(isset($_REQUEST['search']))
{
$search=$_REQUEST['searchtxt'];
some code here
}
回答by Vetrivel
Try the below code it will work.. I have coded without button only.
试试下面的代码它会起作用..我只编码没有按钮。
<?php
if(isset($_POST['search']))
{
echo $search = $_POST['search'];
}
?>
<html>
<head>
<script>
document.onkeydown=function(evt){
var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
if(keyCode == 13)
{
//your function call here
document.test.submit();
}
}
</script>
</head>
<body>
<form name="test" action="#" method="POST">
<input type="text" name="search" />
</form>
</body>
</html>
回答by xReprisal
If I understand you correctly you want to trigger php code on every key press in which case good solution will be using AJAX.
如果我理解正确,你想在每次按键时触发 php 代码,在这种情况下,好的解决方案是使用 AJAX。
$( "#target" ).keypress(function(e) {
$.ajax({
url: "logic.php",
type: post,
data: $('form').serialize(),
success: function( data ) {
//Process data received from server.
}
});
});
On each key press all form data will be serialized and send over to server where you can perform your logic.
在每次按键时,所有表单数据都将被序列化并发送到您可以执行逻辑的服务器。
回答by EternalHour
The button should be submitting on Enter
, if it's not, there's something in the HTML that you have not provided. You have tagged the question with JQuery but have not provided any code, so I can only assume that you are submitting the form through only PHP.
该按钮应该在 提交Enter
,如果不是,则 HTML 中存在您未提供的内容。您已使用 JQuery 标记了问题,但未提供任何代码,因此我只能假设您仅通过 PHP 提交表单。
Take a look at the buttondocumentation. There are two paragraphs there that are relevant to your situation.
查看按钮文档。有两段与您的情况相关。
formHTML5
The form element that the button is associated with (its form owner). The value of the attribute must be the id attribute of a element in the same document. If this attribute is not specified, the element must be a descendant of a form element. This attribute enables you to place elements anywhere within a document, not just as descendants of their elements.
formHTML5
与按钮关联的表单元素(其表单所有者)。该属性的值必须是同一文档中某个元素的 id 属性。如果未指定此属性,则该元素必须是表单元素的后代。此属性使您可以将元素放置在文档中的任何位置,而不仅仅是作为其元素的后代。
This means that for the button to submit the form, it must be inside the form
tags. If it is not, you need to specify the form id like this.
这意味着对于提交表单的按钮,它必须位于form
标签内。如果不是,则需要像这样指定表单 id。
<button form="form_id">
The second relevant piece of information.
第二条相关信息。
type
The type of the button. Possible values are:
submit:The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.
reset:The button resets all the controls to their initial values.
button:The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.
type
按钮的类型。可能的值有:
submit:按钮将表单数据提交到服务器。如果未指定属性,或者属性动态更改为空值或无效值,则这是默认值。
重置:该按钮将所有控件重置为其初始值。
按钮:按钮没有默认行为。它可以具有与元素事件相关联的客户端脚本,这些事件在事件发生时被触发。
Since type
defaults to submit
, it means that the form will submit with the Enter
key.
由于type
默认为submit
,这意味着表单将使用Enter
密钥提交。