gpt4 book ai didi

javascript - 解除绑定(bind)点击事件后,jQuery 开/关方法不起作用

转载 作者:行者123 更新时间:2023-12-02 17:01:51 25 4
gpt4 key购买 nike

我创建了一个处理大量数据的ajax函数,因此当单击按钮时,可能需要一段时间才能响应。现在,当人们访问该网站时,他们不断点击,这使我的数据库留下了重复的数据。我在 Google 上搜索并找到了 jQuery 的 onoff 事件,用于绑定(bind)和取消绑定(bind)点击事件。当函数被触发时,取消绑定(bind)事件起作用,但是当响应无效时,我想再次绑定(bind)单击事件,这在我的情况下不起作用。

警报被触发,因此它到达 on 函数。

有人可以看一下我的函数并看看我做错了什么吗?

    $('#afrekenen').click(function(){
$('#afrekenen').off("click");
clearInterval(myInterval);
var fields = $('.addressform :input');
$.each(fields, function(field,val){
$(val).removeClass('errorInput');
})
var gegevens = {};
var adresform = $('.addressform').serializeArray();
$.each(adresform, function(index, val){
gegevens[this.name] = this.value;
});
if(!$('input[name=payment]:checked').val()){
var betaalwijze = 0;
}else{
var betaalwijze = $('input[name=payment]:checked').val();
}

var voorwaarden = $('input[name=voorwaarden]:checked').val();

$.ajax({
type: 'post',
url: '/inc/afrekenen.php',
data: {"gegevens":gegevens ,"betaalwijze":betaalwijze,"voorwaarden":voorwaarden},
fail: function() {
$('#afrekenen').on("click");
$('#errormsg').html('<div class="alert alert-danger">'+
'<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>Oops something went wrong. please try again</div>');
},
success: function(data) {
response = jQuery.parseJSON(data)
if(response.isValid == false){
alert('hoi');
$('#afrekenen').on("click");
$('#errormsg').html('<div class="alert alert-danger">'+
'<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'
+response.message+'</div>');

$.each(response.fouteVelden, function(index, object){
$('#'+object+'').addClass('errorInput');
});
}else{

if(/^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'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\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(response.message)) {
window.location.href = response.message;
} else {
window.location.href = 'winkelwagen.php?step=afgerond';
}
}
}
});

最佳答案

我认为你应该将事情分开以使其更容易。

此外,您还可以创建一个命名函数作为您的点击事件处理程序回调,您将相应地绑定(bind)/取消绑定(bind)。

$(function () {
// This will toggle the click event handler on/off.
function toggleClickHandler(toggle) {
if (toggle === true) {
$('#afrekenen').on('click', processMyRequest);
} else {
$('#afrekenen').off('click', processMyRequest);
}
}

// This will process your request (AJAX call).
var processMyRequest = function() {
toggleClickHandler(false);
clearInterval(myInterval);

var fields = $('.addressform :input');

$.each(fields, function (field, val) {
$(val).removeClass('errorInput');
});

var gegevens = {};
var adresform = $('.addressform').serializeArray();

$.each(adresform, function (index, val) {
gegevens[this.name] = this.value;
});

// Unrelated to the question:
// Note that before you had the variable "betaalwijze" declared
// twice using var. This is wrong, so I've fixed it.
var betaalwijze;

if (!$('input[name=payment]:checked').val()) {
betaalwijze = 0;
} else {
betaalwijze = $('input[name=payment]:checked').val();
}

var voorwaarden = $('input[name=voorwaarden]:checked').val();

$.ajax({
type: 'post',
url: '/inc/afrekenen.php',
data: {
"gegevens": gegevens,
"betaalwijze": betaalwijze,
"voorwaarden": voorwaarden
},
success: function (data) {
response = jQuery.parseJSON(data);

if (response.isValid === false) {
alert('hoi');
toggleClickHandler(true);

$('#errormsg').html('<div class="alert alert-danger">' +
'<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + response.message + '</div>');

$.each(response.fouteVelden, function (index, object) {
$('#' + object + '').addClass('errorInput');
});
} else {
if (/^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&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|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(response.message)) {
window.location.href = response.message;
} else {
window.location.href = 'winkelwagen.php?step=afgerond';
}
}
}
}).fail(function () {
// AJAX call has failed.
toggleClickHandler(true);
});
};

// When the document is loaded, attach the click event handler
// by default.
toggleClickHandler(true);
});

关于javascript - 解除绑定(bind)点击事件后,jQuery 开/关方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25620504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com