Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery getting data from form

I have a form that I'm trying to get data from a form using jquery and validate it. What is the best way to take data from a form to a variable using jquery?

like image 871
gilbertbw Avatar asked Aug 07 '12 23:08

gilbertbw


People also ask

How can I get form data with javascript jQuery?

The serializeArray() method creates an array of objects (name and value) by serializing form values. This method can be used to get the form data. Parameter: It does not accept any parameter.

How do I get FormData values?

get() The get() method of the FormData interface returns the first value associated with a given key from within a FormData object. If you expect multiple values and want all of them, use the getAll() method instead.

What jQuery form method can you use to retrieve the input field's data?

The jQuery val() function and serialize() function are built-in functions in jQuery. The val() function can be used to get the first element's value attribute in the set of matched elements. The serializeArray() function is used to get from data; it returns the array of objects by serializing the form data.

How can store form data in jQuery?

You may do something like this: $('#myform'). submit(function(e) { e. preventDefault(); // don't submit the form var form = $(this); // store it for later reference form.


1 Answers

Here is the snippet that you can use -

$('#myForm').submit(function() {
    // get all the inputs into an array.
    var $inputs = $('#myForm :input');

    // not sure if you wanted this, but I thought I'd add it.
    // get an associative array of just the values.
    var values = {};
    $inputs.each(function() {
        values[this.name] = $(this).val();
    });

});

Got it from stackoverflow-link

like image 194
devang Avatar answered Sep 28 '22 18:09

devang