[英]Initiate action by pressing enter
http://protected-river-1861.herokuapp.com/ Link to my site http://protected-river-1861.herokuapp.com/链接到我的网站
So, I need to let the search of Google Images initiate on the press of the 'enter' key. 因此,我需要按“ enter”键启动对Google图片的搜索。 Currently, it only works on the click of the button.
当前,仅在单击按钮时有效。
HTML: HTML:
<p>Enter the keyword to search for images</p>
<input id="search-term" type="text">
<button id="go-search">Go!</button>
<div id="search-results"></div>
<div style="width: 150px; margin:0 auto;">
application.js: application.js:
$(document).on('click', '#go-search', function() {
findImagesOnGoogle({
keywords: $('#search-term').val(),
container: '#search-results'
})
});
$(document).on('click', '#search-results img', function() {
var url = $(this).data('url');
$("#workspace img").remove();
var img = $("<img>").attr('src', url);
$("#workspace").append(img);
});
The CSS, JS and HTML are all on separate tabs in Sublime Text. CSS,JS和HTML都位于Sublime Text中的单独选项卡上。
You can try the following: 您可以尝试以下方法:
$(document).keyup('#search-term', function(event){
if(event.keyCode == 13){
$("#go-search").click();
}
});
It binds a button up listener on your document and triggers a click if enter is pressed from within the input box. 如果在输入框中按下Enter键,它将在您的文档上绑定一个按钮侦听器,并触发单击。
Though I would rather you create a form around the elements and do the needful on it's submit event. 尽管我希望您围绕这些元素创建一个表单,并对其提交事件进行必要的处理。 This way, enter press would automatically be captured as a form submit.
这样,enter press将被自动捕获为表单提交。 (Along with the click on a button of type submit.)
(同时单击提交类型的按钮。)
You could try: 您可以尝试:
$('#search-term').bind('keyup', function(e) {
var code = e.keyCode || e.which;
if(code == 13) {
$('#go-search').click();
}
});
$('#go-search').click(function() {
findImagesOnGoogle({
keywords: $('#search-term').val(),
container: '#search-results'
})
});
http://jsfiddle.net/gummiah/Ga2dG/ http://jsfiddle.net/gummiah/Ga2dG/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.