我在让这个 JQuery AJAX 显示在网页上时遇到问题。
$(document).ready(function(e) {
$(".autocomp").autocomplete(
{
source: function( request, response ) {
$.ajax({
url: "/apples",
dataType: "json",
data: {
srch:request.term
},
success: function( data ) {
response( $.map( data.result, function( item ) {
return {
label: item.name,
value: item.name,
data: item
};
}));
}
});
},
minLength: 2,
select: function(event, ui){
var parts = this.name.match(/(\D+)(\d+)$/);
$("key_"+parts[2]).val(ui.item.data.key);
}
});
});
该函数调用一个返回信息的网站: {result: [{name:macintosh,key:1},{name:golden,key:2},{name:fuji,key:3}]}
. 在数据字段中发回的对象是:
Object {result= [Object{name:macintosh,key:1},Object{name:golden,key:2},Object{name:fuji,key:3}]}
的HTML:
<input type="text" name="name_1" id="name_1" class="autocomp"><input type="hidden" name="key_1" id="key_1">
任何人都知道为什么它不起作用?我正在进口
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"></script>
和样式表:
<link href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
任何帮助将不胜感激!