Print all form values in JQuery with the console?

0

Is there any way to capture all the data selected in the selects and inserted into the inputs of a form?

I've tried using $('#form1').serialize(); but I did not get a result, in php I use the global variable $_GET and $_POST and I use var_dump() to print the results, I wanted to know how can I do this in jquery? >     

asked by anonymous 29.09.2017 / 22:11

2 answers

3

Just serialize with serializeArray and transform into JSON .

var dados = JSON.stringify( $(#form1).serializeArray() ); //  <-----------

console.log( dados );
    
29.09.2017 / 22:22
1

Well from what I saw the serialize () did not work, then how do you just get the values of the inputs and the selects. You can use $ .each (). As follows:

$(function(){
  var print=function(){
    var data = {};
    $('#form1 input, #form1 select').each(function(){
      data[$(this).attr("name")] = $(this).val();
    });
    console.log(data);
  };

  print(); // irá printar o objeto no console
});

Basically what this code does is go in every select and every input present inside the form, get the attribute name and its value. Then add this to the data object such that o, the key is the field name (which can be input or select) and the value is the value of the field.

Enclosed within a function, it can be called when and however you need it.

I hope I have helped;)

    
29.09.2017 / 22:39