I have two input fields like this:
<input name="accountCode" class="accountCode grid_2"/>
<input name="accountCode" class="accountCode grid_2"/>
I want to have an autocompleter on both of these fields. I have written the following JavaScript:
$(".accountCode").autocomplete(
{
minLength : 1,
source : function(request, response) {
$.ajax({
url : baseUrl + "Autocomplete/Account?accountCode=" + $(this).val(),
dataType : "json",
success : function(data) {
response($.map(data, function(item) {
return {
value : item.accountCode,
desc : item.accountName
}
}));
}
});
},
focus : function(event, ui) {
$(this).val(ui.item.accountCode);
return false;
},
select : function(event, ui) {
// $("#category").val( ui.item.name );
$(this).val(ui.item.value);
// $( "#project-description" ).html( ui.item.desc );
return false;
}
}).data("autocomplete")._renderItem = function(ul, item) {
return $("<li></li>").data("item.autocomplete", item).append(
"<a><strong>" + item.value + " </strong>" + item.desc + "</a>")
.appendTo(ul);
};
Of course, my server returns JSON data with 2 field: accountCode and accountName.
I want both inputs to use the custom renderer in _renderItem so that this will be displayed in the list:
"<a><strong>" + item.value + " </strong>" + item.desc + "</a>"
For the first field, it works perfectly, but for second field it only displays the accountCode from item.value.
I've checked that the JSON received from the server is the same in both cases so the problem is in the Javascript.
Do you know why this problem exist?