我有HTML:
<form id="form1" name="form1" method="post" action="">
<input name="PtName" type="text" id="PtName" />
<input name="Button" type="button" id="button" onclick="search_p()" value="Check" />
</form>
serach_p()是函数:
<script type="text/javascript">
function search_p(){
$.ajax({
url: 'srchpt.php',
type: 'POST',
data: { PtName: $('#PtName').val()},
success: function(data){
$(".myresult").html(data);
}
})
}
</script>
我想在PtName文本中按Enter键时执行相同的search_p()函数 我怎么能这样做?
答案 0 :(得分:1)
在表单上指定onsubmit
:
<form ... onsubmit="search_p(); return false">
并将按钮的type
更改为submit
:
<input name="Button" type="submit" id="button" value="Check" />
答案 1 :(得分:0)
您可以使用以下jquery函数来完成此操作:
$("#PtName").keyup(function (e) {
if (e.keyCode == 13) {
// call function
}
});
答案 2 :(得分:0)
Javascript: 在javascript中放入以下函数
function enterPressed(event) {
var key;
if (window.event) {
key = window.event.keyCode; //IE
} else {
key = event.which; //firefox
}
if (key == 13) {
yourFunction();
// do whatever you want after enter pressed event. I have called a javascript function
}
}
HTML:
<input type="text" onkeypress="javascript:enterPressed(event)">
针对所需的文本字段放置onkeypress
事件
答案 3 :(得分:0)
在表单的提交事件中调用您的函数: -
<html>
<head>
<script type="text/javascript">
function search_p(){
$.ajax({
url: 'srchpt.php',
type: 'POST',
data: { PtName: $('#PtName').val()},
success: function(data){
$(".myresult").html(data);
}
})
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="" onsubmit="search_p()" >
<input name="PtName" type="text" id="PtName" />
<input name="Button" type="button" id="button" onclick="search_p()" value="Check" />
</form>
</body>
</html>
答案 4 :(得分:0)
我想要一个文本区域,该文本区域会在 shift + enter 上换行,并在 Enter 上提交: This seems to answer my query