Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

294
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda