This is my form:
<form action="/Admin/usuarios/obtener_valores" method="POST">
<div class="span-16">
<input type="hidden" value="1" name="usuarioPinIdHidden" id="usuarioPinIdHidden">
<table class="summary" style="border: none;">
<tr>
<td colspan="2">¿A que edad tuvo sus primeras vacaciones?</td>
</tr>
<tr>
<td colspan="2"><input id="text@'+1+^+1" name="respuesta[0].respuesta" type="text" value=""/></td>
</tr>
<tr>
<td colspan="2">¿Estaba casado en 1991?</td>
</tr>
<tr>
<td colspan="2"><input id="boolean@'+3+^+1" name="respuesta[1].respuesta" type="radio" value="true"/>
<label for="boolean@'+3+^+1">Si</label>
<br />
<input id="boolean@'+3+^+1" name="respuesta[1].respuesta" type="radio" value="false"/>
<label for="boolean@'+3+^+1">No</label></td>
</tr>
</table>
</div>
This is a function to print all the id values after clicking the submit button:
$(document).ready(function() {
$('form').submit(function (){
return verificarRespuestas();
});
});
function verificarRespuestas(){
alert('hola');
$('form').children().each(function(){
var child = $(this);
alert(child.id);
});
return false;
}
but this is what it's printed:
when I expect to get these values:
What am I doing wrong?
Thanks in advance!
children
only selects the immediate children to the element, you want $('form').find('input')
or $('form').find('input[id]')
to only get those that have an ID, or $('form').find('input:visible')
to only get those that are visible (ie, not hidden.)this.id
in the each
. Or, through jQuery, $(this).attr('id')
. There are many smarter ways to denote field types in your inputs if you are doing some sort of client-side validation or styling, most commonly through classes but also through custom attributes or field types in HTML5. IDs are definitely not where you want to be putting booleanXXX
information. The HTML spec says:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
You should use the jQuery attr()
function to get the id.
jQuery.attr() Get the value of an attribute for the first element in the set of matched elements.
alert(child.attr('id');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With