如何在我的表单操作中使用jQuery .ajax [英] How to use jQuery .ajax to my form's action

查看:86
本文介绍了如何在我的表单操作中使用jQuery .ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更改了php和jQuery的编码风格,但是我的注册

I changed my coding style for php and jQuery, but my Registration

$("#reg_form_company").bind("submit", function() {
    $.fancybox.showActivity();
    $.ajax({
            type     : "POST",
            cache    : false,
            url      : $(this).attr('action'),
            data     : $(this).serializeArray(),
            success  : function(data) {
                $(".printArea").empty().append(data).css('visibility','visible');
            }
    });
    return false;
});

然后这是我的表格

<form id="reg_form_company" action="index.php?module=register&actionregister" method="post" >
      <input>[...]</input>
</form>

然后单击提交"按钮后,它不起作用,我认为有人可以帮助我解决此问题,因为$ .ajax可能会使POST(用于输入)和GET(用于参数)混淆操作"表单中的内容)

Then after clicking the "Submit" button, it doesn't work, I assume that somebody can help me to solve this problem, coz the $.ajax might confuse about POST(for inputs) and the GET(for the parameters of the "action" form)

感谢您的帮助,如果需要,您还可以修改整个jQuery代码.

I appreciate for your help, you can also modify the entire jQuery code if it's required.

抱歉,您不包括#reg_form_company和fancybox

Sorry guys for not including the #reg_form_company, and the fancybox

推荐答案

您需要执行以下操作: http://jsfiddle.net/xSJTs/2/

You need to do something like this: http://jsfiddle.net/xSJTs/2/

$('form').on('submit',function(e){
    e.preventDefault();
    $.ajax({
        type     : "POST",
        cache    : false,
        url      : $(this).attr('action'),
        data     : $(this).serialize(),
        success  : function(data) {
            $(".printArea").empty().append(data).css('visibility','visible');
        }
    });

});

您必须使用serialize()而不是serializeArray(). serializeArray()创建一个JavaScript对象,serialize()创建一个查询字符串.

You have to use serialize() instead of serializeArray(). serializeArray() creates a JavaScript-object, serialize() creates a query-string.

序列化: http://api.jquery.com/serialize/

SerializeArray: http://api.jquery.com/serializeArray/

SerializeArray: http://api.jquery.com/serializeArray/

基本上,您要等到提交表单后再中断表单(e.preventDefault();).

Basically you wait until the form is submitted and then you interrupt it (e.preventDefault();).

这篇关于如何在我的表单操作中使用jQuery .ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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