我正在尝试将所有表单字段传递给 ajax 函数,我将在其中将用户插入数据库。
但由于某种原因,我的警报(在我的 JS 文件中)没有显示任何内容。
你知道我做错了什么吗?
我的 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>
我的 JavaScript 文件:
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
}
}
);
}
}
最佳答案
看起来您正在序列化元素本身。您必须序列化表格,请检查一下。
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
}
}
);
}
}
关于javascript - 将表单字段发送到 jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23940357/