我想創建一個簡單的搜索框,允許您根據您在輸入字段中輸入的關鍵字進行搜索。如果按下「提交」按鈕,此工作正常。jquery輸入/按鍵不工作
我也想能夠按回車鍵或Return鍵開始搜索。我試過使用.on函數,它似乎只能用於點擊,但在輸入或按鍵時沒有成功。 任何有識之士將不勝感激!
$(document).ready(function() {
//$("#gallery").empty();
$("#submit").on("click keypress",function (e) {
console.log($(this).text());
console.log("e.which " +e.which);
console.log("e.type " +e.type);
if (e.which === 13 || e.type === 'click') {
if($("#term").val() !=""){
//$("#gallery").empty();
/**********************/
updatestatus();
ajaxProcess();
/**************************/
}else {
//$("#gallery").text("Please enter a keyword to search").fadeOut(2500);
alert("Please enter a keyword to search");
}
}
});
$("#clear").click(function(){
$("#gallery").empty();
});
});
HTML
<form>
<input type="text" value="" id="term" />
<input id="submit" type="button" value="Go">
<input type="reset" id="clear" value="Clear" />
</form>
那麼你擊中'#submit'按鈕進入,或者在'#term'輸入?您應該可以將一個事件綁定到每個元素。 – adeneo 2015-04-05 16:59:58
甚至更好,將事件附加到submit'事件的表單,並且它會自動工作 – adeneo 2015-04-05 17:00:26
@ user244394我已經展示了將表單提交以及綁定按鍵事件同時輸入到文本框的示例。檢查我的回答 – mohamedrias 2015-04-05 17:15:24