weixin_33743880 2014-05-29 18:25 采纳率: 0%
浏览 9

发送表单字段到jQuery

I'm trying to pass all my form fields, to a ajax function where i will insert the user into the database.

But for some reason, my alert (in my JS file) isn't showing anything.

Any ideas what i'm doing wrong?

My HTML:

        <form id="signupForm">
            <input id="signupFormEmail" type="text" name="email" placeholder=" E-mail"><br />
            <input id="signupFormPassword" type="text" name="password" placeholder=" Password"><br />
            <input id="signupFormUsername" type="text" name="userName" placeholder=" User Name"><br />
            <input id="submitSignup" type="button" value="SIGN UP" onclick="signUp(this);">
        </form>

My javascript file:

function signUp(elem)
{   
var postData = $(this).serializeArray();

//$('#myResults').html(postData);
alert($.param($(elem).serializeArray()));

if($(elem).parent().children('#signupFormEmail').val() != ''){      
    // verifica se o email já existe
    $.ajax(
    {
          url: "/newsletter/check-email/",
          type: "POST",
          data: {type:'check',email:$(elem).parent().children('#signupFormEmail').val()}
    }).done(function(response)
    {   
        if(response == -1) {
            $.ajax(
            {
              url: "/newsignup/registare/",
              type: "POST",
              data: postData
            }).done(function(userCreated) {
                if(userCreated == 1) {
                alert('user created');
                    /*
                    $(elem).parent().children('#signupForm').val('');
                    $('#signUpCompleted').show();
                    */
                } 
                else
                {
                    /*$('#signUpError').show();*/
                    alert('user not created');
                }
            })

            //testing
            //$('#signUpCompleted').show();
        }
        else //testing
        {
            $('.emailError').show(); //testing
        }

    }
    );
}
}
  • 写回答

2条回答 默认 最新

  • weixin_33743703 2014-05-29 18:28
    关注

    It looks like you are serializing the element itself. You have to serialize the form, please check this out.

    function signUp(elem)
        {   
        var postData = $('form').serialize();
        //$('#myResults').html(postData);
        alert(postData);
    
        if($(elem).parent().children('#signupFormEmail').val() != ''){      
            // verifica se o email já existe
            $.ajax(
            {
                  url: "/newsletter/check-email/",
                  type: "POST",
                  data: {type:'check',email:$(elem).parent().children('#signupFormEmail').val()}
            }).done(function(response)
            {   
                if(response == -1) {
                    $.ajax(
                    {
                      url: "/newsignup/registare/",
                      type: "POST",
                      data: postData
                    }).done(function(userCreated) {
                        if(userCreated == 1) {
                        alert('user created');
                            /*
                            $(elem).parent().children('#signupForm').val('');
                            $('#signUpCompleted').show();
                            */
                        } 
                        else
                        {
                            /*$('#signUpError').show();*/
                            alert('user not created');
                        }
                    })
    
                    //testing
                    //$('#signUpCompleted').show();
                }
                else //testing
                {
                    $('.emailError').show(); //testing
                }
    
            }
            );
        }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助