2012-02-23 39 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