Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
How to get form data with multi select in checkboxs - javascript

I have a form that I want to get all his values, and convert them to json object.

the problem is that I not get the multiselect fields, just one field. this is my code what I am missing?

let reportDropDowns = $('#getReport').serialize();
        reportDropDowns = decodeURI(reportDropDowns);
        let reportDropDownsJson = {};

   
    reportDropDownsJson =  JSON.parse('{"' + reportDropDowns.replace(/&/g, '","').replace(/=/g,'":"') + '"}', function(key, value) { return key===""?value:decodeURIComponent(value) });

this is my html enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Instead of serializing the data directly via .serialize(), you should instead use .serializeArray() to get the data as an array. You can then manipulate the array into an object/JSON. Try this

let reportDropDownsJson = {};
$('#getReport').serializeArray().forEach(({name, value}) => {
    if( reportDropDownsJson.hasOwnProperty(name) ){
        if( !Array.isArray(reportDropDownsJson[name]) ){
            reportDropDownsJson[name] = [reportDropDownsJson[name]];
        }
        
        reportDropDownsJson[name].push(value);
    }else{
        reportDropDownsJson[name] = value;
    }
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!