jquery自动完成json响应没有填满文本框
问题描述:
我得到了正确的json响应,并且我把javascript警报检查了一下,但是无法使用响应将数据填充到文本框中,为什么下面的代码没有返回项目列表到文本框下拉菜单。jquery自动完成json响应没有填满文本框
response($.map(data, function (item) {
alert(item.task_id);
// getPreventDefault();
return { label: item.module_task, value: item.task_id};
}))
不工作,即它不填写文本框,即使我item.task_id和item.module_task正确的成功块内。 **
enter code here
(function($){
$(document).ready(function(){
$(document).on('click', '.autocomplete', function(){
$('[id^="repairRecordsTask"]').each(function() {
$(this).autocomplete({
minLength:3,
open: function() {
// After menu has been opened, set width to 100px
$('.ui-menu')
.width(100);
} ,
source : function(request, response) {
//alert("inside the autocomplete ");
$.ajax({
// alert("inside ajax");
url : "<%=request.getContextPath()%>/Task_Search_Controller.do",
type : "GET",
minLength: 3,
contentType: "application/json; charset=utf-8",
autoFill: true,
//term is the input from request object
data : {
term : request.term
},
dataType : "json",
success : function(data) {
alert(data.toString());
response($.map(data, function (item) {
alert(item.task_id);
// getPreventDefault();
return { label: item.module_task, value: item.task_id};
}))
},
select: function (event, ui) {
alert("here");
$(this).val(ui.item.module_task);
//$(this).val(ui.item.value);
//$("#txtAllowSearchID").val(ui.item.value);
}
/* error: function(jqXHR, textStatus, errorThrown){
//alert("error is " + errorThrown.toString());
// alert("error is " + textStatus);
// alert("json reponse is " +jqXHR.responseJSON);
// alert("json reponse is " +jqXHR.responseJSON);
}*/
});
}
});
});
$(document).on('autocompleteselect', '.autocomplete', function(event, ui) {
// alert("selected " + ui.item.value);
var selectedTask = ui.item.value;
lookUpDMCodeRea(selectedTask);
});
});
});
})(jQuery);
和我的JSON看起来像这样
my json is like this
var data =[
{
"task_id": "1539",
"module_task": "810-01"
},
{
"task_id": "1540",
"module_task": "810-02"
},
{
"task_id": "1541",
"module_task": "810-04"
},
{
"task_id": "13175",
"module_task": "810-04"
}
]
答
我认为源应该是服务器代码的网址你在哪里得到的JSON,这是为我工作,用PHP
jQuery(this).autocomplete({
source: "<?php echo site_url("search_users.php"); ?>",
minLength: 1,
select: function(event, ui) {
//do something when a item is selected
console.log(ui.item.id);
}
}).data("uiAutocomplete")._renderItem = function(ul, item) {
return jQuery("<li></li>")
.data("item.autocomplete", item)
.append(
"<a style='cursor:pointer;'>"+
"<table>" +
"<tr>" +
"<td width='30'>" +
"<img src= '"+item.img+"' width='20' height='25' /> " +
"</td>" +
"<td>" +
item.label +
"</td>" +
"</tr>" +
"</table>"+
"</a>"
)
.appendTo(ul);
};
和我的JSON是:
[
{
"label": "Juan Perez",
"value": "Juan Perez",
"id": 25,
"img": "http://localhost/admin/resources/as45265f."
},
{
"label": "Juan Perez",
"value": "Juan Perez",
"id": 25,
"img": "http://localhost/admin/resources/as45265f.jpg"
},
{
"label": "Juan Perez",
"value": "Juan Perez",
"id": 25,
"img": "http://localhost/admin/resources/as45265f.jpg"
},
{
"label": "Juan Perez",
"value": "Juan Perez",
"id": 25,
"img": "http://localhost/admin/resources/as45265f.jpg"
}
]
不,我的代码工作正常,直到它从ajax调用返回json对象后,我得到了上面显示的格式的json后,响应无法在文本框中设置 – skalluri 2014-11-05 21:04:01
有人可以帮我解决方案或任何替代方案解 – skalluri 2014-11-06 11:11:38