原諒我,如果這很簡單 - 我已經遠離電腦和JS一個月了,所以這個任務對我來說似乎是不可能的,在那裏我知道它不應該'不要。jQuery:檢測'change'事件處理程序中是否點擊某個按鈕
我能想到的最快捷的解釋就是把它放在代碼中,用註釋來解釋它是如何工作的。我會在這些評論中指出哪些是我無法弄清楚的部分。
我有一個看起來不可編輯的文本字段,旁邊有一個「編輯」按鈕,點擊後變爲「保存」按鈕,並使文本字段看起來可編輯。
$(".edit_btn").click(function() {
// make the associated text field look editable, and change this 'edit'
// button into a 'save' button. Then place focus on the text in
// in the field.
});
$(".save_btn").click(function() {
// if (value in associated text field has changed from when page loaded)
// submit the form and save this new text
// else
// revert to non-editable mode (hide this 'save' button,
// and show 'edit' button)
});
所有的工作正常。但是,如果用戶離開可編輯文本字段,我希望能夠檢測他們是否已離開字段以單擊「保存」按鈕,或者只是單擊頁面上的其他任何位置。因此,在該字段的「模糊」事件處理程序中,我需要知道是否點擊了「保存」按鈕。對於我的生活,我無法弄清楚如何做到這一點:
$('input[name^="tgfld_"]').blur(function() {
// if (save button has been clicked) <- this is the problem
// don't do anything since the save function will handle this
// else if (value in the field hasn't changed)
// revert everything back to non-editable mode
// else if (value in the field HAS changed)
// do a window.confirm and prompt the user to click the 'save'
// button to save their changes
});
所以它的檢測,如果保存按鈕是爲「模糊」被觸發的原因 - 這就是我想不通。或者,如果這完全是處理這種情況的錯誤方式,請讓我知道。
(我應該指出的是,可能有許多頁面上這些字段/按鈕組合,但至今未影響到任何東西。)
這有幫助嗎? http://stackoverflow.com/q/6411271/497356 –