Value of a query to a numeric field returns non-numeric

3

I am trying to read a field in a table that is of integer type. And it's also a primary key and auto-increment.

The returned value is put in a label (already tried to put in an input text too).

To read the target table and put the result in the label, I use a JQuery statement. The event is clicked on

<input type='file':

Click code is

jQuery("#idFilFoto").click(function (){
var nSigla=jQuery("#idLblSigla").text();
var tabela = "TabPessoas";
var clausulaWhere = " TabPessoasSigla = '"+nSigla+"'";
var jqxhr = $.post("genericaPegaID.php",{tabela:tabela,clausulaWhere:clausulaWhere},      function(resultado) {
jQuery("#idLblID").text(resultado);
});//fim do post
});//fim do click

This works fine.

No label

<label id="idLblID" 

the value is set correctly. An integer without spaces before or after appears.

Now, I want this value inside that label to use it in another table.

This table is populated by the click event of a button.

jQuery("#idBtnSalvar").click(function (){
var tabela="TabFotos";
var campos="TabFotosCaminhoArquivo,TabFotosFKPessoas";
var matricula = jQuery("#idLblID").text();
var foto = jQuery("#idFilFoto").val();
var posicao = foto.indexOf("fakepath");
foto = foto.substr(posicao+9);
var valores="'"+foto+"',"+matricula;    
alert (tabela+" : "+campos+" : "+valores);
/*
var jqxhr = $.post("genericaInserir.php",{tabela:tabela,campos:campos,valores:valores}, function(resultado) {
alert("success "+resultado);
});//fim do post
*/
});//fim do botao salvar

The commented block above is because the 'values' field forces an error in the Insert Into statement that I have in the GenericInserir.php page. It says that the value of the 'enrollment' field is non-numeric.

Actually, if you inspect what was going to the 'post' (see the alert before the commented block) appears, for example, like this:

'photo.png', [] [] 6.

Please interpret the two brackets as two closed symbols, like two rectangles.

It should be just the figure 6, in this case.

I tried

var matricula = parseInt(jQuery("#idLblID").text(),10);

but the result is NaN no alert.

    
asked by anonymous 23.03.2014 / 15:11

1 answer

6

You are collecting the value of the element with the identifier #idLblID using the .text() method. (English) that serves the purpose of collecting a string with everything found within the target element.

The documentation describes that there are differences in browser content collection for the browser, which may explain why you are picking up characters beyond the expected number:

  

The result of the .text () method is a string containing the combined text of all matched elements. (Due to variations in the HTML parsers in different browsers, the text returned may vary in newlines and other white space.)

What translated:

  

The result of the .text () method is a string containing the combined text of all localized elements. (Due to variations in the HTML interpreters of different browsers, the returned text may vary in new lines and in another blank space.)

The difference essentially depends on your markup , but if it's nothing more than a number between the tags in your element, the way you're getting that value should also work:

<label id="idLblID">10</label>

JSFiddle Demo

Solution

Without looking at your markup to analyze what comes when you collect the value, my solution is to use $.trim() that serves to clear the collected value by removing whitespace and others:

  

The $ .trim () function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string.

What translated:

  

The $ .trim () function removes all line breaks, spaces (including non-separable spaces), and tabs at the beginning and end of the given string.

HTML with tabs, blanks, and number 10:

<label id="idLblID">     10 &nbsp;    </label>

Value collection and verification:

var numero = $.trim($('#idLblID').text()); // recolhe conteúdo

alert(numero);                             // alerta 10

alert(parseInt(numero) || 0);              // alerta  10

Demonstration in JSFiddle

    
23.03.2014 / 15:45