最初,我遇到了一个点击事件多次触发的问题,但我已经设法克服了这个问题,可能会过度使用unbind()
和one()
,您会在我的代码中看到!需要清除功能存储器。 jQuery运行功能太多了
我在这里有一些代码,它打开了一个通用的Modal窗口,我用它来做各种事情,包括在某些情况下的密码表单。
我不认为你需要HTML,所以我不会发布。
当一个按钮,或一个动作导致需要的窗口,我调用该函数是这样的:
showModalAlert(type, theWidth, theHeight, title, html, confirmThis, denyThis)
前三个变量决定的窗口的外观,title
和html
确定的内容和confirmThis
和denyThis
是在调用此功能之前立即设置的功能,并确定如果这是confirm
窗口和confirm
或或deny
按钮按下时应采取的操作。
在security
窗口的情况下,确认按钮被替换为“签名”按钮,该按钮提交简单的密码表单并从数据库返回用户标识。如果用户标识成功返回,则脚本以编程方式按下confirm
按钮,并依次调用模态窗口的初始打开。
我的问题是,如果输入的密码不正确,或用户稍后取消了窗口,然后在不刷新浏览器窗口,重新进入正确的密码,进行两次(或多次的confirmThis()
功能因为执行了错误的密码/取消操作)。
因此,显然,它所做的是每次“记住”confirmThis
函数。
正如我所说的,最初,密码成功函数点击confirmIt
两次,丰富使用one()
已经修复了这个,现在肯定只能点击一次confirmIt
,但它仍然是执行功能多的时间。
如何清除此功能并确保只执行一次?
功能从我打电话模态窗口如下所示:
$('#saveDelivery').click(function() {
function confirmIt() {
formData = (JSON.stringify($('#delDetail').serializeObject()));
saveData(formData);
$('#saveDelivery').removeClass('centreLoader');
};
showModalAlert('security', '300px', '185px', 'Security!', 'You need to "Sign" this action.', confirmIt, '');
});
它只是在saveDelivery
元素上点击时,confirmThis
功能在此时宣布并提交一个AJAX形式
实际showModalAlert
功能如下:
function showModalAlert(type, theWidth, theHeight, title, html, confirmThis, denyThis) {
// stuff that opens the alert window \\
if (confirmThis == '') {
$('#confirmIt').one('click', function() { $('#closeAlert').one('click').click(); });
} else {
$('#confirmIt').one('click', function() { confirmThis(); $('#closeAlert').one('click').click(); });
};
if (denyThis == '') {
$('#denyIt').one('click', function() { $('#closeAlert').one('click').click(); $('#signIt').unbind(); });
} else {
$('#denyIt').one('click', function() { denyThis(); $('#closeAlert').one('click').click(); $('#signIt').unbind(); });
};
if (type == "confirm") {
$('.closeAlert, .signItForm').hide();
};
if (type == "alert") {
$('.alertConfirm, .signItForm').hide();
};
if (type == "fixedAlert") {
$('.closeAlert, .alertConfirm, .signItForm').hide();
};
if (type == "security") {
$('.signItForm').show();
$('.closeAlert').hide();
$('#confirmIt').hide();
$('#signIt').unbind().fadeTo('fast',1);
};
};
$('#signIt').live('click', function() {
var formData = (JSON.stringify($('.secureSign').serializeObject()));
var signitPwd = $('#signItpwd').val();
var jsonURL = "/jsonout/getdata.aspx?sql=SELECT id, password FROM users WHERE password ='" + signitPwd + "' LIMIT 1&output=json&usedb=new&labelName=any&fileName=";
$.getJSON(jsonURL, function (data) {
if (data.length > 0) {
$('.savingUserID').val(data[0].id);
$('#confirmIt').one('click').click();
$('#signIt').fadeTo('fast', 0);
$('#confirmIt').show();
} else {
$('#signIt').fadeTo('fast', 0);
$('#confirmIt').one('click').show();
$('.closeAlert').show();
$('.alertConfirm, .signItForm').hide();
$('#alertTitle').html("Error!");
$('#alertContent').css({ 'text-align': 'center' }).html("Password Denied");
};
});
});
Spot on!非常感谢,只需将该行添加到模态函数的顶部即可。尝试删除一些其他'unbind()'调用以及因为它们可能不需要! – 2012-02-23 12:42:56
Sorted!删除了所有其他'unbind()'引用,现在它完美地工作!:D – 2012-02-23 12:46:13
好一个:)!很高兴我能帮上忙! – Benno 2012-02-23 12:47:23