Walking through HTML form elements with jquery script, I see that I'm able to get the tag's id with this code:
$(":checkbox,:radio,:text,:selected", this).each(function(){
var controlName = $(this).attr("id");
var controlType = $(this).attr("type");
alert(controlName);
if(controlType=="checkbox" || controlType=="radio"){
if ($(this).attr('checked')=="checked"){cadena += "&" + controlName + "=on";}
else{ cadena += "&" + controlName + "=off";}
}
else{
cadena += "&" + controlName + "=" + $(this).attr('value');
}
});
It works with checkboxes, radioButtons and textInputs, giving me the right ids. But for select tags it always return "undefined".
What I'm doing wrong?
Thanks.
* UPDATE * This is my current form. I know that it is not a "normal" form, but I'm using it in a special application. Thanks to everybody for your patience:
<form action="#" id="ejemplo" class="ejemplo">
<fieldset>
<legend> Formulario de ejemplo </legend>
<input id="Nombre" type="text" value="Texto1" name="Nombre" /> <label for="Nombre">Nombre</label> <br/>
<input id="Apellidos" type="text" value="Texto1" name="Apellidos" /> <label for="Apellidos">Apellidos</label> <br/>
<label for="campo2">Campo 2</label>
<select id="coches">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
<br/>
<input type="checkbox" id="option1"> <label for="option1"> Milk </label>
<input type="checkbox" id="option2"> <label for="option2"> Butter </label>
<input type="checkbox" id="option3"> <label for="option3"> Cheese </label>
<br/>
<input type="radio" name="group1" id="radio1"> <label for="radio1"> Milk </label> <br/>
<input type="radio" name="group1" id="radio2"> <label for="radio2"> Butter </label> <br/>
<input type="radio" name="group1" id="radio3"> <label for="radio3"> Cheese </label>
<br/>
<textarea cols="40" rows="5" id="areaTexto">Inside de text area!</textarea>
<br/>
<input id="boton" type="button" value="Boton"><br>
</fieldset>
</form>
Getting the select "coche" id, it returns "undefined". But getting its value return the correct one.
After run My code it returns that:
&Nombre=Texto1&Apellidos=Texto1&undefined=volvo&option1=off&option2=off&option3=off&radio1=off&radio2=off&radio3=off
Look at undefined=volvo :S