我的问题可能听起来有点愚蠢,但我需要知道你是否可以为同一个按钮设置click()和按键功能。例如,这是我的代码:
$("#Submit").click(function(event){
event.preventDefault();
var th = '<tr><th>' + "Business" +'</th><th>' + "Address"+ '</th><th>'+ "Rating" + '</th><th>' + "Date" + '</th></tr>';
$("#restaurants").empty().html(th);
var Searching = $("#Search").val();
$.ajax({
type : "GET",
url : "http://www.cs.kent.ac.uk/people/staff/lb514/hygiene/hygiene.php",
dataType : "json",
data : {op : "searchname", name : Searching},
success : function(data){
if(data.length < 1 ){
$("#restaurants").empty().html(th);
var response = confirm(" This search term : " + Searching + " was not found in our database. Please try another search ");
if(response){
$('#Search').val("");
}
$("#restaurants").html(th);
}else if(data.length > 1){
$.each(data,function(key,results){
$("#restaurants").append(
"<tr><td>" + results.business +"</td>"+
"<td>" + results.address + "</td>" +
"<td>" + results.rating + "</td>" +
"<td>" + results.date + "</td></tr>")
})
$('#Search').val("");
}
}
});
})
此功能目前仅通过单击“提交按钮”触发。如果我在填写搜索文本后按下回车按钮则不起作用。那么有没有办法通过点击和输入来创建一个函数触发器?
答案 0 :(得分:1)
当您专注于搜索文本时,您可以在用户背面按住按键单击按住回车键。它的工作原理如下:
{{1}}
答案 1 :(得分:0)
$('#Search').keyup(function(e){
var e = event || window.event; // for modern browser and IE
if(e.keyCode == 13){ // charCode for enter key
$("#Submit").click(); // when it is checked, then triggers the submit btn
}
});
答案 2 :(得分:0)
试试这个:
$('.input').keypress(function (e) {
if (e.which == 13) {
$('form#login').submit();
return false;
}
});