Send form data using ajax

前端 未结 7 937
遇见更好的自我
遇见更好的自我 2020-12-01 04:45

I want to send all input in a form with ajax .I have a form like this.

相关标签:
7条回答
  • 2020-12-01 05:37

    as far as we want to send all the form input fields which have name attribute, you can do this for all forms, regardless of the field names:

    First Solution

    function submitForm(form){
        var url = form.attr("action");
        var formData = {};
        $(form).find("input[name]").each(function (index, node) {
            formData[node.name] = node.value;
        });
        $.post(url, formData).done(function (data) {
            alert(data);
        });
    }
    

    Second Solution: in this solution you can create an array of input values:

    function submitForm(form){
        var url = form.attr("action");
        var formData = $(form).serializeArray();
        $.post(url, formData).done(function (data) {
            alert(data);
        });
    }
    
    0 讨论(0)
提交回复
热议问题