2
I have the following code snippet:
var arrayPost = $("#formPlano13").serializeArray();
var arrayCBDDD = $("#formPlano13 input[name='cbDDD[]']").serializeArray();
var trDDD = '';
// * Geramos um loop para os DDDs escolhidos
$.each(arrayCBDDD, function(k, v){
qtdDDD = $('input[name=txQtdDDD' + v.value + ']').val();
qtdPortabilidade = $('input[name=txQtdPortabilidadeDDD' + v.value + ']').val();
trDDD += '<tr><td><strong>DDD ' + v.value + '</strong><br />Qtd Números: ' + qtdDDD + '<br /></td><td>R$</td></tr>';
if(qtdPortabilidade > 0){
trDDD += '<tr><td><strong>Portabilidade DDD ' + v.value + ':</strong><br />';
varNumero = $('input[name=\'txPortabilidadeNumero' + v.value + '[]\']');
for(i = 0; i < varNumero.length; i++){
trDDD += '- ' + varNumero[i].val() + '<br>';
}
trDDD += '</td></tr>';
}
});
console.log(arrayPost);
console.log(trDDD);
ArrayPost values (can dynamically increase or decrease)
0: {name: "cbDDD[]", value: "011"}
1: {name: "txQtdDDD011", value: "1"}
2: {name: "cbPortabilidade011", value: "on"}
3: {name: "txQtdPortabilidadeDDD011", value: "1"}
4: {name: "txPortabilidadeNumero011[]", value: "(11) 11111-1111"}
ArrayCBDDD values (can dynamically increase or decrease)
0: {name: "cbDDD[]", value: "011"}
When executing it, I’m getting the error: $(...)[i]. val is not a Function
How can I capture the value of the varNumber that comes from this loop? I’m having problems because this input is a multiple array (it can receive several values).
I think what you’re looking for is
varNumero.eq(i).val()
orvarNumero[i].value
. You can test?– Sergio
varNumero[i]
will return an html element, which does not have the function.val()
. In this case, you can access with.value
, or, if you want to continue with the objectjquery
, use.eq(i)
– Artur Trapp
@Sergio always faster than me haha :)
– Artur Trapp
@Arturotemplario :)
– Sergio
I’ll test and I’ll be right back! D
– Maykel Esser
Worked perfectly!
– Maykel Esser
I know it sounds stupid what I’ll ask, but how do I mark your answer, @Sergio as answered?
– Maykel Esser
@Maykelesser the question is quite common. I’ve already marked it as duplicate from another previous question. You don’t need to do anything else :)
– Sergio
Thank you very much! ;-)
– Maykel Esser