Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

285
Vistas
Javascript FormData object Ignoring some fields (under certain circumstances)

I have a dynamic form that is building and rebuilding categories based on the users input. Basically it is a product finder that narrows down the results as more categories are checked. I am mostly doing this with jQuery, but I found this piece of code that takes the form data and turns it into JSON.

var fData = new FormData(document.querySelector('#prodFilter'))
// Build Filters
buildFilterData(fData);


function buildFilterData(e) {
    const object = {};
    e.forEach((value, key) => {
        // Reflect.has in favor of: object.hasOwnProperty(key)
        if(!Reflect.has(object, key)){
            object[key] = value;
            return;
        }
        if(!Array.isArray(object[key])){
            obFOrject[key] = [object[key]];    
        }
        object[key].push(value);
    });

    // Stringify Result
    let formResult = JSON.stringify(object);
    params = formResult;
    return params;
}

This little snippet is cool because it can handle multiple checked values. I get a result like:

{"keyword":"","product_type":"Category","product_category":["Sub-Cat1","Sub-Cat2"]}

This is/was working great until I tried to add a keyword search box to further narrow-down results within the selected categories. As soon as I try and populate that text input it drops all the checkbox data (the categories are still checked). I am logging my parameters as the form is submitted and I get something like this:

{"keyword":"ER 8"}

The weird part is that if I type a keyword into the box, then start checking categories it picks up all the data and I get:

{"keyword":"ER 8","product_type":"Category","product_category":["Sub-Cat1","Sub-Cat2"]}

Anyone have any idea why the checkbox/radio fields are being dropped? Hopefully I have provided plenty of information, I'm happy to clarify anything I can.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

After a bunch of trial and error, we figured out that the enter submit wasn't picking up the checkboxes for some reason. It was triggering the submit event and running the subsequent processes, just not feeding all the inputs to the function in the question. The solution was to intercept the enter key and then submit the form via jQuery .submit().

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda