最初,我遇到了一個點擊事件多次觸發的問題,但我已經設法克服了這個問題,可能會過度使用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