[英]JQuery POST executes twice
我有一個注冊表單,我為檢查數據庫中的 email 是否存在做了一些條件,但是當我使用現有的 email 創建一個新帳戶時,它返回我 1 即(電子郵件存在),這沒關系,但是當我創建一個新帳戶時新鮮的 email 它再次返回我 1.. 它應該返回我 -> 4 那是為了(成功創建帳戶)請參閱下面的代碼:
$('#acord').on('change', function(){
this.value = this.checked ? 1 : 0;
$("#formreg").submit(function(event) {
event.preventDefault();
$(".inreg").html('<i class="fa fa-spinner fa-pulse"></i> Verificam informatiile..');
var $form = $(this),
acord = $form.find("[type='checkbox'][name=acord]").val(),
nume = $form.find("[type='text'][name=nume]").val(),
email = $form.find("[type='email'][name='email']").val(),
parola = $form.find("[type='password'][name='parola']").val(),
varsta = $form.find("[type='text'][name='varsta']").val(),
url = 'inc/sql/register-user.php';
setTimeout(function() {
var posting = $.post(url, {
nume: nume,
email: email,
parola: parola,
varsta: varsta,
acord: acord
});
posting.done(function(data) {
if (data == 1) {
swal({
title: "Adresa de mail exista",
text: "Adresa de email "+email+" este deja folosita.",
type: "error",
timer: 4000,
showConfirmButton: false
});
$(".inreg").html('INREGISTREAZA-TE');
} else if (data == 2) {
swal({
title: "Ups!",
text: "Adresa de email nu este valida.",
type: "error",
timer: 4000,
showConfirmButton: false
});
$(".inreg").html('INREGISTREAZA-TE');
} else if (data == 3) {
swal({
title: "Ups!",
text: "Parola prea lunga, trebuie sa contina intre 5 si 10 caractere. ",
type: "error",
timer: 4000,
showConfirmButton: false
});
$(".inreg").html('INREGISTREAZA-TE');
} else if (data == 4) {
swal({
title: "Felicitari! " + nume + "",
text: "Contul a fost inregistrat in baza noastra de date. Nu uitati sa adaugati momentele. Va dorim mult succes!",
type: "success",
timer: 2100,
showConfirmButton: false
});
$(".inreg").html('<i class="fa fa-spinner fa-pulse"></i> Va logam automat in cont..');
setTimeout(function() {window.location.href = "?p=contul-meu";}, 3000);
} else if (data == 5) {
swal({
title: "Termeni si conditii neacceptate!",
text: "Ne pare rau, dar ca sa te poti inregistra pe aceasta platforma trebuie sa fi de acord cu termenii si conditiile noastre!",
type: "warning",
timer: 4300,
showConfirmButton: false
});
}
$(".inreg").html('INREGISTREAZA-TE');
})
}, 3000);
});
}).change();
$('#acord').on('change', function(){
this.value = this.checked ? 1 : 0;
$("#formreg").submit(function(event) {
您正在更改偵聽器中添加一個新的表單提交偵聽器。
$('#acord').on('change', function(){ ... }).change();
您還在注冊更改偵聽器后立即觸發更改事件(正如我們在上面看到的那樣,它將向您的表單添加一個提交偵聽器)。
然后,我假設您正在填寫您的表單並手動單擊#accord
,這將為您的表單添加第二個提交偵聽器。 然后你提交表單,你的兩個處理程序都被調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.