2012-02-23 46 views
1

最初,我遇到了一个点击事件多次触发的问题,但我已经设法克服了这个问题,可能会过度使用unbind()one(),您会在我的代码中看到!需要清除功能存储器。 jQuery运行功能太多了

我在这里有一些代码,它打开了一个通用的Modal窗口,我用它来做各种事情,包括在某些情况下的密码表单。

我不认为你需要HTML,所以我不会发布。

当一个按钮,或一个动作导致需要的窗口,我调用该函数是这样的:

showModalAlert(type, theWidth, theHeight, title, html, confirmThis, denyThis) 

前三个变量决定的窗口的外观,titlehtml确定的内容和confirmThisdenyThis是在调用此功能之前立即设置的功能,并确定如果这是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"); 
    }; 
}); 
}); 

回答

1

从我了解$.one,它只是运行事件一次。如果您将它绑定到事件两次,它将立即运行两次,但不会再发生。

例如:http://jsfiddle.net/qCwMH/(单击按钮,它将运行事件4次)。

每次单击saveDelivery时,您都会将其他$.one事件绑定到#confirmIt

你可以做的是在模态函数的开始(即$('#confirmIt, #denyIt').unbind('click');,解锁你的事件,从confirmIt和denyIt中解除绑定,然后每次调用这个函数时都会分配给它们,而不是建立在它们之上。理想的,因为绑定/解除绑定使用比其他选项更多的资源,但只是试着从头开始?

+0

Spot on!非常感谢,只需将该行添加到模态函数的顶部即可。尝试删除一些其他'unbind()'调用以及因为它们可能不需要! – 2012-02-23 12:42:56

+0

Sorted!删除了所有其他'unbind()'引用,现在它完美地工作!:D – 2012-02-23 12:46:13

+0

好一个:)!很高兴我能帮上忙! – Benno 2012-02-23 12:47:23