使用.on("submit", [英] Jquery Ajax form using .on("submit",

查看:153
本文介绍了使用.on("submit",的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,上面有几个html表单.下面是通过ajax调用提交表单的jquery代码.提交内容div中的任何表单时,它都会进行ajax调用.即使通过ajax重新加载了表单,它也不会触发.我不知道如何将表单数据传递给处理程序函数.任何帮助将不胜感激.

I have a page with several html forms on it. Below is the jquery code to submit the forms via an ajax call. It does make the ajax call when any form in the content div is submitted. It does fire even after the form is reloaded via ajax. I cannot figure out how to pass the form data to the handler function. Any help would be greatly appreciated.

要澄清:var formData = $(this).serializeArray();不管用. formData不能从表单文件中获取值.

To Clarify: var formData = $(this).serializeArray(); is not working. formData is not getting values from the form fileds.

$.ajaxSetup({
  url: "ajax.php",
  type: 'POST',
  contentType: "application/json; charset=utf-8",
  datatype: 'json',
  cache: false,
  timeout : 5000
});
$("#content").on("submit", function(e) {
  e.preventDefault();
  var formData = $(this).serializeArray();
  $.ajax({
    data: formData,
    success: function(jsonData) {
        htmlData = jQuery.parseJSON(jsonData);
        $.each(htmlData, function(id, data){
            $("#"+id).html(data);
        });
    }
});
};

更新:

如果我更改

$("#content").on("submit", function(e)

$("#content").on("submit", "form", function(e)

表单数据被序列化,并且ajax函数按预期工作.新的问题是,如果表单不完整,并且将其重新加载(通过ajax)到内容分区中,则Submit事件不再触发该函数.我试图将.on()配置为与.live()在早期版本的jquery中一样.重新加载ajax之后,它将重新绑定Submit事件.在这种情况下,.on()不会重新绑定.希望有道理!

the form data is serialized and the ajax function works as intended. The new problem is that if the form is not complete and it is reloaded (via ajax) into the content division, the submit event no longer triggers the function. I was trying to configure .on() to function the way .live() did in earlier versions of jquery. It would rebind the submit event after the ajax reload. In this case, .on() is not rebinding. Hope that makes some sense!

推荐答案

您需要通过$ _POST全局变量获取ajax.php中传递的变量.

You'll need to get the passed variables in ajax.php through $_POST globals.

例如:

$myVar = $_POST['field_name'];

这篇关于使用.on("submit",的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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