Jquery getting form data

I have a form in which I am trying to get data from a form using jquery and validate it. What is the best way to transfer data from a form to a variable using jquery?

+5
source share
4 answers

Here is a snippet 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 fooobar.com/questions/17967 / ...

+5
source

Ok here we go

This is a jQuery script:

function getData () {

$(document).ready(function() {
   var myTextFieldValue = $('#myTextField').value;
   alert('The value is: '+myTextFieldValue);
 });

}

This is HTML

<form action='#' method='whatever'>
 <input type='text' id='myTextField' />
 <input type='submit' onClick='getData()' />
</form>

Note: You must import jQuery libraries to make your script work .

Like this:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>

I have not tried the script, so there may be some errors. I hope I was helpful to you bye.

(For any help pm me)

+2

. . <input id='some-id' > var someId = $('#some-id').val();. someId

val() ( , , jquery (http://api.jquery.com/category/selectors/) http://api.jquery.com/val/, val, - <textarea>

, , js securrity

0

JQuery.

0

All Articles