有人可以幫我嗎? 的想法是創建一個循環的動態按鈕,然後使用jQuery的點擊功能,使用其中一個jquery動態點擊功能
//I'm creating dynamic buttons like this:
for(i=0; i<1000; i++){
$contentBox.append('<button id="add'+ i +'" type="button" class="btn btn-success">Accept</button>');
//but how would I create the jquery click function?
$('#add'+i).click(function(e) {....});
//this does not create 1000 click functions. It only changes the id to the last one so what ever button you click on you will always get the las id
}
您將希望使用'$(document).on('click','#add'+ i',function(e){...} )'動態元素。你可以參考關於[委託事件]的SO文檔(http://stackoverflow.com/documentation/jquery/1321/events/7666/delegated-events#t=201612170400446260567)。我有一個例子,特別是將事件處理程序添加到動態元素,儘管它從未被接受/刪除。無論如何,只要將文件示例中的$('ul')'更改爲'$(document)',它就可以在任何情況下工作。 –
使用代表。 '$(document).on(click:function(){},'#add'+ i)' – Nadeem