[title]Javascript jQuery 回车事件 触发 Ajax?[/title]
这里收集一段jQuery 代码,一个输入框,回车时发生搜索,而我们这里的结果是跨域得出来的,用到了jsonp 类型。具体代码如下:
// 搜索框,会车时触发事件
$("#search_input").keydown(function(e){
var that = $(this).val();
if(( e.which == 13 && $.trim(that.length) > 0 )){
e.preventDefault();
var data = {};
data.keyword = that ;
$.ajax({
type: "get",
url: http + '/search/index/keyword/',
data: data ,
cache: "false",
dataType: "jsonp",
async: false,
success: function(data){
// console.log(data);
if(data.status){
window.location.href = '/search/index/keyword/' + that;
}
},
error: function(){
alert('请求数据错误,请稍后再试');
}
});
}
});