Quiero recorrer los elementos de un formulario HTML y almacenar los valores de los campos < de entrada > en un objeto. El código siguiente no funciona, sin embargo:Bucle sobre elementos en jQuery
function config() {
$("#frmMain").children().map(function() {
var child = $("this");
if (child.is(":checkbox"))
this[child.attr("name")] = child.attr("checked");
if (child.is(":radio, checked"))
this[child.attr("name")] = child.val();
if (child.is(":text"))
this[child.attr("name")] = child.val();
return null;
});
Ni hace lo siguiente (inspirado por la respuesta de jobscry):
function config() {
$("#frmMain").children().each(function() {
var child = $("this");
alert(child.length);
if (child.is(":checkbox")) {
this[child.attr("name")] = child.attr("checked");
}
if (child.is(":radio, checked"))
this[child.attr("name")] = child.val();
if (child.is(":text"))
this[child.attr("name")] = child.val();
});
}
La alerta se referirá child.length == 0
. La selección manual de los elementos de obra:
>>> $("#frmMain").children() Object length=42 >>> $("#frmMain").children().filter(":checkbox") Object length=3
¿Alguna pista sobre cómo hacerlo en la horquilla?
Su hilo simplemente me salvó 20 minutos de la línea de campos de entrada de línea para múltiples formas -_- ;. +1. – zeboidlund