I have this code and works well;
$(".campodigitavel").change(function(){
$(".campocalculado").each(function(){
if ($(this).attr("ordem") == 1){ // O valor do primeiro item do grupo será o campo base para o cálculo.
lnTotal = $(this).val();
}
// Percorre todos os filhos cujo pai é o atual da iteração
$("[pai=" + $(this).attr("id") + "]").each(function(){
if(this.value != ""){
lnTotal = eval(lnTotal + "" + $(this).attr("operador") + "" + parseFloat(this.value));
}
});
$("#" + $(this).attr("target")).val(lnTotal);
});
});
But, when I alter the code adding the a function call like this:
$(".campodigitavel").change(function(){
$(".campocalculado").each(function(){
if ($(this).attr("ordem") == 1){ // O valor do primeiro item do grupo será o campo base para o cálculo.
lnTotal = $(this).val();
}
// Percorre todos os filhos cujo pai é o atual da iteração
$("[pai=" + $(this).attr("id") + "]").each(function(){
if(this.value != ""){
lnTotal = eval(lnTotal + "" + $(this).attr("operador") + "" + parseFloat(this.value));
}
});
$("#" + $(this).attr("target")).val(lnTotal);
});
calcularPercentual();
});
The function code:
function calcularPercentual(){
var lnValorReferencia, lnValorPai
lnValorReferencia = $("[percentual=true]").val();
$('.perccalculado').each(function(){
lnValorPai = $("#" + $(this).attr("pai")).val();
if(lnValorPai != ""){
$(this).val(lnValorPai * 100 / lnValorReferencia);
}
});
}
When the JQuery Code execute in second time, appear the error: Uncaught SyntaxError: Unexpected token ILLEGAL.
Why?
Thanks, Luciano
4 Answers 4
add (;) after var lnValorReferencia, lnValorPai;
Comments
You forgot semicolon (;) after initializing var lnValorReferencia, lnValorPai
Comments
I discover the erro after read this topic ( http://forum.jquery.com/topic/chrome-uncaught-syntaxerror) I open the page in firefox and it show me the true error.
I changed the name of atribute pai to paiperc on the fields with class="perccalculado".
Comments
Maybe try putting a semi-colon after the line:
var lnValorReferencia, lnValorPai
I'm kinda doubtful on this since js does a lot of implicit semicolon insertion, but worth a try
<input type="text" id="delegadoperc" size="5" class="perccalculado" pai="delegado" readonly> <input type="text" id="presidenteperc" size="5" class="perccalculado" pai="presidente" readonly> <input type="text" id="hsperc" size="5" class="perccalculado" pai="hs" readonly>A total of 11 fields.Uncaught SyntaxError: Unexpected token ILLEGAL (anonymous function)TelaFixacaoMetas.html:56 e.extend.eachjquery-1.7.1.min.js:2 e.fn.e.eachjquery-1.7.1.min.js:2 (anonymous function)TelaFixacaoMetas.html:54 e.extend.eachjquery-1.7.1.min.js:2 e.fn.e.eachjquery-1.7.1.min.js:2 (anonymous function)TelaFixacaoMetas.html:49 f.event.dispatchjquery-1.7.1.min.js:3 f.event.add.h.handle.i