How can I get form data with JavaScript/jQuery?

前端 未结 28 2378
不知归路
不知归路 2020-11-22 12:39

Is there a simple, one-line way to get the data of a form as it would be if it was to be submitted in the classic HTML-only way?

For example:



        
28条回答
  •  面向向阳花
    2020-11-22 13:22

    function getFormData($form){
        var unindexed_array = $form.serializeArray();
        var indexed_array = {};
    
        $.map(unindexed_array, function(n, i){
            if(indexed_array[n['name']] == undefined){
                indexed_array[n['name']] = [n['value']];
            }else{
                indexed_array[n['name']].push(n['value']);
            }
        });
    
        return indexed_array;
    }
    

提交回复
热议问题