2015-05-31 49 views
2

为什么我不能在“on”处理程序中将变量写入选择器。怎么会是对的?内容可以在ajax请求后,所以我需要写$(文档)而不是$(按钮)!?jquery查找后点击元素

+1

我认为on()背后的基本思想是将动态元素上的事件绑定......为什么你想在之前找到它。 –

+2

因为这就是'.on()'的工作原理。如果你传递一个选择器作为它的第二个参数,它必须是一个字符串。你的'buttons'变量将引用一个jQuery对象,该对象包含在线运行时存在于'.sector' *中的所有按钮元素。* - 它不包括在ajax请求之后添加的未来元素。 – nnnnnn

+0

狡猾但'var buttons ='.sector button';'。你最好的选择是'$(document).on('click','.sector button',function(){...})' – lshettyl

回答

1

您可以将它绑定到您要通过ajax更新内容的div父级。

$('#ParentDivOfUpdateId').on('click', '.sector button', function() { 
    // here ParentDivOfUpdateId is Id of div which is parent to your dynamic content 
    //... 
})