简体   繁体   English

Ajax插入后,表单未序列化

[英]Form doesn't serialize after Ajax insertion

Tearing my hair out over this. 撕掉我的头发。 I have a 40 rows of simple forms that are being generated dynamically from a mysql database. 我有40行简单表格,这些表格是从mysql数据库动态生成的。 Each form has a unique ID based on the database ID. 每个表单都有一个基于数据库ID的唯一ID。 After clicking submit the results get updated in the database and inserted into the div (#result). 单击提交后,结果将在数据库中更新并插入div(#result)。

Works the first time perfectly. 第一次完美工作。 However after the first time the script won't serialize the updated form data. 但是,在第一次执行后,脚本将不会序列化更新后的表单数据。 The ID is fine (checked via alert) but the formData is empty (also checked via alert). ID很好(通过警报检查),但formData为空(也通过警报检查)。

Thinking I need to re-target the form somehow? 以为我需要以某种方式重新定位表单? Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

$('#result').on('click', '.submitform', function () {
    var id = $(this).attr('id');
    var formData = $('#'+id+'-form').serialize();
    $.ajax({
        type: "POST",
        url: "ajax-process-form.php",
        data: formData,
        cache: false,
        success: function(server_response){
            $("#result").html(server_response).show();
        }
    });
    return false;
});

Just reasoning... I might be wrong... 只是推理...我可能错了...

This code 这段代码

$('#result').on('click', '.submitform'

binds to the click event on result and filters .submitform , then executes with this being the .submitform 绑定到click事件上的结果并过滤.submitform ,然后执行this作为.submitform

when the success comes from the server, you are rewriting #result 当成功来自服务器时,您正在重写#result

$("#result").html(server_response)

if server_response does not contain a .submitform then next calls to the first onclick event will not execute because .submitform does not exist anymore inside #result 如果server_response不包含.submitform则将不会执行对第一个onclick事件的下一次调用,因为.submitform中不再存在#result

If this is the error, then to solve, use another div to show the result instead of #result or bind click to another separated, not contained within div 如果这是错误,请使用另一个div来显示结果,而不是#result或将click绑定到另一个不包含在div中的分隔#result来解决

Arrgh - it was the structure after all. Arrgh-毕竟是结构。 Although it worked the first time the table structure prevented it from working a second time. 尽管它是第一次工作,但是表结构阻止了它第二次工作。 I have no idea why ... but there you go. 我不知道为什么...但是你去了。 Thanks for the help! 谢谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM