This question already has answers here:
Event binding on dynamically created elements?
                                
                                    (23个答案)
                                
                        
                5年前关闭。
            
        

我在表单上有两个按钮。按钮和表单都是动态创建的:

      container.html(........
                     ........
                     <input type='button' value='Send' id='btSend' />"+
                    "       <input type='button' value='Save' id='btSave' />"+
                     ................
                     ...............)


这两个函数应该收集击键:

       $("#btSend").click(function(){
        ..........
        ............
        }

        $("#btSave").click(function(){
        ..........
        ............
        }

最佳答案

您可以使用.on()click事件委托给动态创建的元素。见下面的代码-

$(document).on("click","#btSend",function(){
        ..........
        ............
        }

 $(document).on("click","#btSave",function(){
        ..........
        ............
 }

09-19 20:35