gpt4 book ai didi

javascript - 使用 Enter 键提交 JQuery 表单

转载 作者:可可西里 更新时间:2023-11-01 00:02:38 24 4
gpt4 key购买 nike

我尝试过各种解决方案,但没有一个适合我的具体情况!我怎样才能做到这一点,以便当用户按下“Enter”时,表单会提交和搜索。

顺便说一句,我正在使用带有 AJAX 的烂番茄 API。

表格

<form name="myform" action="" method="GET"><h3 class="white">Search for a movie here!:</h3><br> 
<input type="text" id="inputbox" value="">&nbsp;
<input type="button" id="button" value="Go!" onClick="filmlist(this.form)">
</form>

ajax.js

function filmlist(form) {
$('#films table').empty(); //removes previous search results before adding the new ones.
var apikey = "APIKEY";
var baseUrl = "http://api.rottentomatoes.com/api/public/v1.0";
var moviesSearchUrl = baseUrl + '/movies.json?apikey=' + apikey;
var query = form.inputbox.value; //uses the value from the input box as the query search
$(document).ready(function () {
// sends the query
$.ajax({
url: moviesSearchUrl + '&q=' + encodeURI(query),
dataType: "jsonp",
success: searchCallback
});
});
// receives the results
function searchCallback(data) {
$('#films table').append('Found ' + data.total + ' results for ' + query);
var movies = data.movies;
$.each(movies, function (index, movie) {
$('#films table').append('<tr><td width="70" rowspan="2"><a href="' + movie.links.alternate +
'" title="Click here to view film information for ' + movie.title + '."><img class="ajaximage" src="' + movie.posters.thumbnail + '" /></a></td><td class="ajaxfilmlisttitle"><h3><a href="' + movie.links.alternate +
'" title="Click here to view film information for ' + movie.title + '.">' + movie.title + '</a></h3>Release year: ' + movie.year + '</td></tr><tr><td class="ajaxfilmlistinfo">Audience Score: ' + movie.ratings.audience_score +
'<br>' + 'Cinema Release Date: ' + movie.release_dates.theater +
'<br>Runtime: ' + movie.runtime + ' minutes</td></tr>');
});
}
}

最佳答案

我会试一试,这是一个更完整的解决方案,适用于整个“听按键输入以提交”,我在跨浏览器工作时遇到了一些问题,也许这会为你解决。

根据您的代码,这应该可以正常工作。

  $('#inputbox').live("keypress", function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
e.preventDefault();
e.stopPropagation();
$(this).closest('form').submit();
}
});

关于javascript - 使用 Enter 键提交 JQuery 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21639495/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com