jQuery bind event does not work at all

后端 未结 3 1088
栀梦
栀梦 2021-01-16 06:41

I did everything I could to make it happen, but without success.

The problem is that I create an element on runtime then bind a function to the element like the fo

3条回答
  •  迷失自我
    2021-01-16 07:08

    Change

    $('#runtime').bind('click',func_name());
    

    to

    $('#runtime').live('click',func_name); 
    

    or (as of jQuery 1.7):

    $('#runtime').on('click',func_name); 
    

    Two things to note:

    1. I changed func_name() to func_name. You don't want to call the function when you bind the handler - you just want to reference it.
    2. Calling bind won't do you any good, because #runtime doesn't exist until after you've clicked .rem. That's why you need either live or on (depending upon your jQuery version).

    And just for good measure: here's a good reference on why you should be using jQuery.on anyway.

提交回复
热议问题