Javascript 如何在表单中实现输入键操作
在我的php应用程序中,我使用textfield和按钮[search] 当我输入一些数据并按下回车键时,按钮动作不起作用 有人能帮忙吗 我的代码是Javascript 如何在表单中实现输入键操作,javascript,jquery,html,Javascript,Jquery,Html,在我的php应用程序中,我使用textfield和按钮[search] 当我输入一些数据并按下回车键时,按钮动作不起作用 有人能帮忙吗 我的代码是 <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> <script> function clearerrormessage()
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
<script>
function clearerrormessage()
{
document.getElementById('info').innerHTML="Enter a keyword to find prospective customer";
}
function submi()
{
/*$('#keyword').keyup(function(e) {
if (e.keyCode == 13) {
$('#form1').submit();
}
});*/
keyword1 =document.getElementById('keyword').value;
//var unvalidkey = new Array("is","am","a","b",'',"are","was","were");
var unvalidkey = new Array("is","am","a","b",'',"are","was","were","+","/","%",".","&","\\","\"","'","?","#");
for(keyitem in unvalidkey)
{
if(unvalidkey[keyitem]==keyword1)
var f=0;
}
if(f==0)
{
document.getElementById('info').innerHTML = '<h3><font color = red>Please enter a valid keyword to search</font></h3';
return false;
}
keyword=keyword1.replace(' ','_');
$.post( "<?php echo base_url() ?>ajax/followsearch.php", { keyword : keyword })
.done(function( data ) {
$("#content1").html("<div id ='content'></div>");
$('#content').scrollPagination(
{
nop : 30, // The number of posts per scroll to be loaded
offset : 0, // Initial offset, begins at 0 in this case
error : 'No Results To Display!', // When the user reaches the end this is the message that is
// displayed. You can change this if you want.
delay : 500, // When you scroll down the posts will load after a delayed amount of time.
// This is mainly for usability concerns. You can alter this as you see fit
scroll : true // The main bit, if set to false posts will not load as the user scrolls.
// but will still load if the user clicks.
}
);
});
}
</script>
</head>
<body>
<form id="form1" name="form1" action="" />
<div id="followkeyword">
<input type="text" style="width:300px;height:26px" name="keyword" id="keyword" onClick="clearerrormessage()" />
<div id="info1"><span id="info">Enter a keyword to find prospective leads</span>
</div>
</div>
<div id="followperloc">
<input type="button" name="mysubmit" id="mysubmit" value="Display my prospective leads" onclick="return submi()"/>
</div>
</form>
</body>
</html>
函数clearerrormessage()
{
document.getElementById('info').innerHTML=“输入关键字以查找潜在客户”;
}
函数subm()
{
/*$(“#关键字”).keyup(函数(e){
如果(e.keyCode==13){
$('表格1')。提交();
}
});*/
关键字1=document.getElementById('keyword')。值;
//var unvalidkey=新数组(“is”、“am”、“a”、“b”、“are”、“was”、“were”);
var unvalidkey=新数组(“is”、“am”、“a”、“b”、“are”、“was”、“was”、“were”、“+”、“/”、“%”、“\”、“\”、“\”、“\”、“?”、“\”);
用于(未验证密钥中的关键项)
{
if(unvalidkey[keyitem]==关键字1)
var f=0;
}
如果(f==0)
{
document.getElementById('info')。innerHTML='请输入有效关键字以搜索检查事件键代码13以检测enter键。请尝试以下操作:
Html:使用onkeypress
代替onclick
,因为onclick
是一个鼠标事件
<input type="button" name="mysubmit" id="mysubmit" value="Display my prospective leads"
onkeypress="return submi(event)" onclick="return submi(event)"/>
为什么您的函数名为submi()
?这似乎很奇怪,请使用onkeypress
代替onclick
,因为onclick
是一个鼠标事件。请检查我的更新答案。。
function submi(event) {
event.which = event.which || event.keyCode;
if(event.which == 13 || event.which == 1) {
// wrap up your entire code here
}
}