Javascript 如何通过序列化表单数据发送数据

Javascript 如何通过序列化表单数据发送数据,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,如何为页面中的多个表单发送数据通过序列化表单数据存储特定表单的值 $(function () { $('.form').on('submit', function (e) { console.log($(".form").serialize( )) ; $.post({ type: 'post', url: 'admin/sv_replycomment.php', data: $(this).serialize(),

如何为页面中的多个表单发送数据通过序列化表单数据存储特定表单的值

$(function () {
    $('.form').on('submit', function (e) {
    console.log($(".form").serialize( )) ;
        $.post({
        type: 'post',
        url: 'admin/sv_replycomment.php',
        data: $(this).serialize(),
            success: function (data) {
                $('.form').trigger("reset"); 
                $('#rep_response').html(data);
                }
            });
        e.preventDefault();
        });
    }); 

修改代码以将表单中的所有字段数据发布到PHP

//Ajmal Praveen
    $("#submit").click(function() {
        $.ajax({
            data: $("form").serialize(),
            ...rest
        });
    });

这是因为您的所有表单都可能具有相同的
class=“form”

因此,要仅提交一个包含序列化数据的表单,需要为要提交的表单和
serialize()
data提供唯一的id

例如:

<form id="type1" class="form">
...
</form>

我认为这将解决您的问题,因为使用类选择器将适用于具有相同类的所有表单

这不是该问题的重复,该问题有多个表单,而另一个有一个表单$(“.form”).each(函数(i,e){theForms.push($(e.serialize());})然后,将数据发送到:
data:JSON.stringify(theForms)
sir我使用了上面提到的代码,但这段代码中的问题是它不能存储一个值,它存储的值等于页面中使用的表单数。sir它的工作,但我需要发送一个值,即单个formOops,应该是
var theForms=[]我的问题是,我用它来表示单个表单而不是多个表单它不存储单个值我也这样做,但仍然有一个问题它保存了四个值而不是一个值。我想存储一个值。
$(function () {
    $('#type1').on('submit', function (e) {
        $.post({
            type: 'post',
            url: 'admin/sv_replycomment.php',
            data: $(this).serialize(),
            success: function (data) {
                $('#type1').trigger("reset"); 
                $('#rep_response').html(data);
            }
        });
        e.preventDefault();
        });
    });