JavaScript与Form表单 - 获取form表单的值

根据控件类型获取:

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

});

 

jquery附带属性

方法1:

var values = {};
$.each($('#myForm').serializeArray(), function(i, field) {
    values[field.name] = field.value;
});

方法2:

$('#myForm').submit(function() {
    // Get all the forms elements and their values in one step
    var values = $(this).serialize();

});

 

2

posted @ 2018-11-02 12:03  德丽莎·阿波卡利斯  阅读(176)  评论(0编辑  收藏  举报