通过jquery/ajax在表单提交中传递数据 [英] passing data in form submission via jquery/ajax

查看:207
本文介绍了通过jquery/ajax在表单提交中传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jquery/ajax提交大约10个输入的表单,但是我不知道如何通过ajax的数据参数将数据传递给它.我应该序列化它们吗?

i want to submit a form with about 10 input using jquery/ajax,but i don't know how can i pass the data to it through data parameters of ajax.should i serialize them ?

推荐答案

jQuery.serialize 可以对你有帮助.对于要提交的表单的所有字段,请使用name属性,这一点很重要.相应的代码可能与以下内容有关

The jQuery.serialize can be helpful for you. It is important that you use name property for all fields of the form which you want to submit. The corresponding code can be about the following

$("form#myFormId").submit(function() {
    var mydata = $("form#myFormId").serialize();
    console.log(mydata); // it's only for test
    $.ajax({
        type: "POST",
        url: "myUrlToPostData.php",
        data: mydata,
        success: function(response, textStatus, xhr) {
            console.log("success");
        },
        error: function(xhr, textStatus, errorThrown) {
            console.log("error");
        }
    });
    return false;
});

这篇关于通过jquery/ajax在表单提交中传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆