F
Your code has some problems. Correct them and it should work:1. number_format is not JavaScript function. Replace:number_format(json[i].valor, 2, ',', '.') For json[i].valor.toFixed(2).replace('.',',')andnumber_format(valor, 2, ',', '.') For valor.toFixed(2).replace('.',',')2. O if (datatype != '') { was not closed. Add a lock key } before closing the function where it is:function carregaServico(e) {
var datatype = $(e).attr('data-type');
var q = $(e).val();
if (datatype != '') {
$.ajax({
url: BASE_URL + 'ajax/' + datatype,
type: 'GET',
data: {
q: q
},
dataType: 'json',
success: function(json) {
if ($('.searchresults').length == 0) {
$('#servico').append('<div class="searchresults"></div>');
}
$('.searchresults').css('left', $('#servico').offset().left + 'px');
$('.searchresults').css('top', $('#servico').offset().top + $('#servico').height() + 3 + 'px');
var html = '';
var id = $(e).closest('tr').find('td').eq('0').html().trim().split('-');
for (var i in json) {
html += '<div class="si"><a href="javascript:;" onclick="selectservico(this)" data-servico="' + json[i].valor + '" data-id="' + id + '">' + json[i].nomedesc + ' - ' + 'R$ ' + number_format(json[i].valor, 2, ',', '.') + '</a></div>';
}
$('.searchresults').html(html);
$('.searchresults').show();
}
});
} // <---- FALTOU ISTO PARA FECHAR O IF
}
3. Failed to place the value name between quotation marks and close the bracket:In this line:$('input[name=vlr_servico[' + id[0].trim() + ']').val("R$ " + number_format(valor, 2, ',', '.'));
should be like this: ↓ ↓↓
$('input[name="vlr_servico[' + id[0].trim() + ']"]').val("R$ " + valor.toFixed(2).replace('.',','));
4. In the loop for, this containment is unnecessary:Instead of ' - ' + 'R$ ', can do so ' - R$ '