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 gojs 点击按钮node的position位置进行改变,再次点击回到原来的位置
  • ¥15 计算决策面并仿真附上结果
  • ¥20 halcon 图像拼接
  • ¥15 webstorm上开发的vue3+vite5+typeScript打包时报错
  • ¥15 vue使用gojs,需求在link中的虚线上添加方向箭头
  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥100 求ASMedia ASM1184e & ASM1187e 芯片datasheet/规格书
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么