jquery从表单中获取数据

gil*_*tbw 5 javascript jquery

我有一个表单,我正在尝试使用jquery从表单中获取数据并验证它.使用jquery将数据从表单转换为变量的最佳方法是什么?

dev*_*ang 5

以下是您可以使用的代码段 -

$('#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();
    });

});
Run Code Online (Sandbox Code Playgroud)

stackoverflow-link获得它