如果我按下快速觸發點擊呼叫的按鈕,我正努力使自己的表單能夠防止雙重submision。我試圖在成功後禁用按鈕,但它仍然發送兩次。這裏是我的代碼:Ajax數據表格序列化防止雙重提交
<script>
$(document).ready(function() {
$("button#rezerva").click(function() {
var chkArray = [];
$(".table:checked").each(function() {
chkArray.push($(this).attr("id"));
});
var selected;
selected = chkArray.join(",");
$.ajax({
type: "POST",
url: "http://rezerv.city/engine/app/add_rezervare.php?mese=" + selected,
data: $("form#formular_rezervare").serialize(),
success: function (data) {
switch (data) {
case "nume_error":
$(".msg").html("<p>Vă rugăm completați numele</p>").fadeIn("slow");
break;
case "tel_error":
$(".msg").html("<p>Vă rugăm completați telefonul</p>").fadeIn("slow");
break;
case "email_error":
$(".msg").html("<p>Vă rugăm completați un email valid</p>").fadeIn("slow");
break;
case "tel_numar":
$(".msg").html("<p>Numarul de telefon trebuie sa contina 10 cifre</p>").fadeIn("slow");
break;
case "adaugat":
$('#rezerva").attr('disabled', 'disabled')
var ora = document.getElementById("timepicker1").value;
var zi_aleasa = document.getElementById("zi").value;
var tip = document.getElementById("tipp").value;
var id_local = document.getElementById("id_local").value;
$("#filtru_zi").load("http://rezerv.city/select_tip_rezervare.php?zi=" + zi_aleasa + "&tip=" + tip + "&id=" + id_local);
$(".succes").html("<p class=\'text-center\'>Ati rezervat masa <b>" + selected + "</b> in data de <b>" + zi_aleasa + "</b> la ora <b>" + ora + "</b></p><p class=\'text-center\'><button class=\'btn btn-default\' type=\'button\' id=\'inchide\'>Închide</button></p>").fadeIn("slow");
break;
default:
alert("A aparut o eroare. Va rugam incercati mai tarziu.");
}
},
});
});
});
</script>
你試圖在點擊事件開始後立即禁用按鈕?並且如果調用不成功,則啓用該按鈕。 – OmerBTW 2015-02-09 22:21:53
這是正確的答案,我敢打賭,有一個更優雅的方式來解決這個問題,但現在thnx :) – user3463807 2015-02-09 23:15:11