Digamos que hay numerosos <input> y quería todos los valores como una matriz. Pensé que sería muy fácil simplemente hacer $("input").map(function(obj) { $(obj).val() }) , pero cuando obtengo eso, aparece el error: Uncaught TypeError: Cannot read properties of undefined (reading 'toLowerCase')
Este error está ocurriendo dentro de jQuery aquí:
var rreturn = /\r/g; jQuery.fn.extend({ val: function( value ) { var hooks, ret, isFunction, elem = this[0]; if ( !arguments.length ) { if ( elem ) { hooks = jQuery.valHooks[ elem.type ] || jQuery.valHooks[ elem.nodeName.toLowerCase() ]; ...Cualquier trabajo alrededor? Aquí hay un violín: https://jsfiddle.net/jvbgo94f/1/
Un poco diferente de lo que está buscando, pero la forma estándar de manejar esto es usar FormData .
Si envuelve sus entradas en un elemento de formulario:
<form> <input name="a" /> <input name="b" /> <input name="c" /> </form>Entonces, puedes hacer algo como esto:
const formData = new FormData( document.querySelector('form') ); for (const [key, value] of formData) { console.log({key, value}); }Debe usar $(this) dentro del mapa para acceder a ese elemento en particular.
$("#a").val("1"); $("#b").val("2"); inputs = $(".test").map(function() { return $(this).val(); }); console.log($(inputs)); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input class="test" id="a" /> <input class="test" id="b" /> <input class="test" id="c" />