[英]Rewrite of this code because serialize() doesn't work
I have this ajax jquery code: 我有这个ajax jquery代码:
var form = document.getElementById('frm');
var data_string = form.serialize();
$.ajax({
type: "POST",
url: "my_php_file.php",
data: data_string,
success: function(data) {
}//end success function
}) //end ajax call
This is in an external file called ajax.js. 这是在一个名为ajax.js的外部文件中。
I include ajax.js into an html file called "show.html". 我将ajax.js包含在一个名为“show.html”的html文件中。 I also include jquery.js into show.html
我还将jquery.js包含到show.html中
I have tried getting the serialize to work, but the code is terminated right before executing the serialize. 我已经尝试使序列化工作,但代码在执行序列化之前就被终止了。 I have no idea why.
我不知道为什么。 But I am sure that it is the serialize which is causing it.
但我确信它是导致它的序列化。
Is it possible to make some easy modification to this, so it doesn't use serialize? 是否可以对此进行一些简单的修改,因此它不使用序列化?
Thanks 谢谢
UPDATE: 更新:
This code (from answer below) seems to work partially also, when I alert the "form" variable, the message says "HTML Form Object" so it finds the form. 这段代码(来自下面的答案)似乎也部分工作,当我提醒“表单”变量时,消息显示“HTML表单对象”,因此它找到了表单。 Then when I alert the "data_string" variable, the message says "frm=undefined".
然后当我提醒“data_string”变量时,消息显示“frm = undefined”。
Any ideas why? 有什么想法吗?
var form = document.getElementById('frm');
var data_string = $(form).serialize();
The serialize()
method comes from jQuery. serialize()
方法来自jQuery。 Your statement is failing because form isn't wrapped in jQuery: 您的语句失败,因为表单未包装在jQuery中:
var form = $('#frm');
var data_string = form.serialize();
Or: 要么:
var form = document.getElementById('frm');
var data_string = $(form).serialize();
My guess is that you're referencing the "Traditional" DOM object using getElementByID and not using it through jQuery (which would traverse the form and add the information). 我的猜测是你使用getElementByID引用“传统”DOM对象,而不是通过jQuery使用它(它将遍历表单并添加信息)。 Try using:
尝试使用:
var data_string = $('#frm').serialize();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.