2013-08-20 147 views
0

表單仍在提交的原因是什麼?驗證檢查工作,但如果一切都輸入正確,表單提交和Ajax請求不會觸發。jquery:返回:提交時不起作用 - 表單仍提交

$("#formRegister").submit(function() { 
// Start problem 
    var mode = $("registerMode").val(); 
// End problem 
    var username = $("#registerUsername").val(); 
    var password = $("#registerPassword").val(); 
    var passwordConfirm = $("#registerPasswordConfirm").val(); 
    var avatar = $("#registerAvatar").val(); 

    if (username == 'Username') { 
     $("#registerError").html('You must enter a username'); 
    } else if (password == 'Password') { 
     $("#registerError").html('You must enter a password'); 
    } else if (password != passwordConfirm) { 
     $("#registerError").html('Your passwords did not match'); 
    } else if (avatar == 'Avatar URL') { 
     $("#registerError").html('You must enter the URL for your combine avatar'); 
    } else { 
     $.ajax({ 
      type: "POST", 
      url: "processUsers.php", 
      data: { 
       mode: mode, 
       username: username, 
       password: password, 
       avatar: avatar 
      }, 
      dataType: "JSON", 
      success: function(data) { 
       alert('success!'); 
      } 
     }); 
    } 

    return false; 
}); 
+2

可以請您給HTML太 – Puneet

+0

哪裏變量被宣佈爲「數據」選項? – hex4

+0

您的瀏覽器控制檯中的任何錯誤 –

回答

2

這應該工作

$("#formRegister").submit(function (event) { 
var username = $("#registerUsername").val(); 
var password = $("#registerPassword").val(); 
var passwordConfirm = $("#registerPasswordConfirm").val(); 
var avatar = $("#registerAvatar").val(); 

if (username == 'Username') { 
    $("#registerError").html('You must enter a username'); 
} else if (password == 'Password') { 
    $("#registerError").html('You must enter a password'); 
} else if (password != passwordConfirm) { 
    $("#registerError").html('Your passwords did not match'); 
} else if (avatar == 'Avatar URL') { 
    $("#registerError").html('You must enter the URL for your combine avatar'); 
} else { 
    $.ajax({ 
     type: "POST", 
     url: "processUsers.php", 
     data: { 
      mode: mode, 
      username: username, 
      password: password, 
      avatar: avatar 
     }, 
     dataType: "JSON", 
     success: function(data) { 
      alert('success!'); 
     } 
    }); 
} 
event.preventDefault(); 
}); 
+0

@Guerra ...大聲笑打我10秒...忘了登錄。 – Epiphany

+0

@Epiphany LOL,在這上面更快=]] – Guerra

+0

'return false;'應該也能工作......它與e.preventDefault();和e.stopPropagation() – tomaroo