在我的頁面上,我有一個報表過濾器。表單通過ajax提交,然後我重新加載頁面的一部分。在頁面部分關閉後,jQuery綁定不再在頁面的重疊部分中工作。頁面重新加載後,JQuery .on()綁定不起作用
這裏是我的html:
<input type="button" id="mainButton" value="Reload the container"/>
<div id="reloadable" style="border: 1px solid black;">
<input type="button" class="supplimentalButton" value="You should see alert" />
</div>
這是jQuery的1.9.1綁定:
// this bit works just fine first time.
// but after reload the binding is no longer attached
// and alert is not coming up on button click
$('.supplimentalButton').on('click',function(){
alert($(this).val());
});
// this is what reloads the container
$('#mainButton').on('click', function(){
$('#reloadable').html('<input type="button" class="supplimentalButton" value="Alert does not work anymore" />');
});
這裏是的jsfiddle:http://jsfiddle.net/trailmax/4JEQv/4/
到目前爲止,所有的我已經諮詢看到當時是用.on()
進行綁定。這就是我正在做的。有關這個問題的任何建議?
因爲當你綁定的事件根本不存在的元素!閱讀[on()']的文檔(http://api.jquery.com/on/),它解釋了它。 – epascarello
@epascarello是的,這就是爲什麼我使用'.on()' - 這是文檔中建議的用法之一。 – trailmax
您是否閱讀過文檔中的[「Direct and delegated events」](http://api.jquery.com/on/#direct-and-delegated-events)部分?用一個例子來解釋這一切。 – epascarello