$(document).ready(function () { initAutocomplete(); }); function initAutocomplete() { $("input[autocomplete-for]").autocomplete({ autoFocus: true, source: function (request, response) { var from_url = $(this.element).attr("from-url"); var parent = $(this.element).attr("parent"); var pfId = $("#" + parent).val(); $.ajax({ url: from_url, dataType: "json", type: 'POST', data: { NumMaxItens: 1, FiltroIdPai: pfId, Pesquisa: request.term }, success: function (data) { var resp; resp = $.map(data, function (item) { return { //label: "
" + marcarHTML(item.Text, request.term) + "
", label: marcarHTML(item.Text, request.term), value: item.Value, /*valor do combo*/ id: item.Id } }); response(resp); } }); }, minLength: 0, html: true, select: function (event, ui) { $("#" + $(this).attr("autocomplete-for")).val(ui.item.id); }, }); } function marcarHTML(html, busca) { var htmlBusca = "$1"; return html.replace(new RegExp("(" + busca + ")", "gi"), htmlBusca) }