我尝试了各种解决方案,但没有一个适合我的特殊情况!
我怎样才能这样做,以便当用户按下’Enter’时,表单提交&搜索.
顺便说一下,我正在使用带有AJAX的烂番茄API.
形成
@H_502_9@<form name="myform" action="" method="GET"><h3 class="white">Search for a movie here!:</h3><br>
<input type="text" id="inputBox" value="">
<input type="button" id="button" value="Go!" onClick="filmlist(this.form)">
</form>
ajax.js
@H_502_9@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 @R_467_4045@ion 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 @R_467_4045@ion 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>');
});
}
}
解决方法:
我会给它一个,一个更完整的解决方案,整个’听Keypress进入提交’我遇到了一些问题,同时使它跨浏览器工作,也许这将为你做.
根据你的代码,这应该工作得很好.
@H_502_9@ $('#inputBox').live("keypress", function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) {
e.preventDefault();
e.stopPropagation();
$(this).closest('form').submit();
}
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。