Wednesday, 15 August 2012

javascript - How to get the $(this) to work in JQueryUI autocomplete -



javascript - How to get the $(this) to work in JQueryUI autocomplete -

here fiddle example

i have problem getting $(this) work in source function jqueryui autocomplete. console shows search input isn't able info attribute 'name' before sending out ajax request. there way pass variable "name" data?

$('.input').autocomplete({ source: function (request, response) { var name = $(this).data('name'); console.log(name); $.ajax({ url: url, datatype: "json", data: { 'q': request.term, 'field': name }, success: function (data) { response($.map(data.query.results.json.json, function (item) { homecoming { label: item.name, } })); } }); }, minlength: 2, select: function (event, ui) { $(this).val(ui.item.label); $(this).val(ui.item.label); }, open: function () { $(this).autocomplete("widget").width(400) } });

you should utilize this.element access corresponding input element. this points autocomplete instance itself:

var name = $(this.element).data('name');

javascript jquery html jquery-ui autocomplete

No comments:

Post a Comment