1
我試圖解除單擊事件並單擊按鈕後單擊一次附加另一個單擊事件。嘗試解除綁定單擊事件並附加新事件
<small id="selectall_agriculture" onclick="refineSearch.testing('agriculture')">(select all)</small>
測試函數運行之後,我想selectAllPropertyTypesInGroup()函數來運行,而是測試函數被調用每次。如果我將onclick事件從小標記中移出並從另一個前端控件觸發setGroupHeaderNotAllSelected(),則在setGroupHeaderNotAllSelected()函數中使用$('#selectall_'+ groupName).click()創建的click事件工作。我覺得解除綁定不起作用,因爲點擊事件被硬編碼到小標籤中。有什麼建議麼?
refineSearch.testing = function (groupName) {
console.log("Click event from front end running instead of the one binded");
setGroupHeaderNotAllSelected (groupName);
};
var setGroupHeaderNotAllSelected = function (groupName) {
$('#selectall_' + groupName).unbind('click');
$('#selectall_' + groupName).click(function() {
selectAllPropertyTypesInGroup(groupName);
return false;
});
};
變化。點擊,進行綁定(「點擊」? –
刪除'onclick'屬性,並用不顯眼的JS,而不是附加事件(例如,jQuery的'on()'),然後使用'off()'在添加新事件之前刪除當前事件。 –
@RoryMcCrossan我不認爲'off()'可以「解除綁定」內聯事件處理程序。但是,你可以將'onclick'屬性設置爲null,這將有效地「解除綁定」。 – gforce301