Assigning click event to dynamically added buttons

后端 未结 5 1840
孤街浪徒
孤街浪徒 2020-12-29 17:13

I am creating some buttons dynamically and assigning them IDs.

When ever someone clicks that button I want to collect the ID and from there perform some task.

5条回答
  •  旧巷少年郎
    2020-12-29 17:39

    I did this using delegate(). In my case, a PHP script generates the content of a page from a MySQL database. To each element, a button is added (clicking these buttons can remove the respective element from the database). The removal is handled by another PHP script which these buttons activate, so a click event is attached to each button like this:

    $('.items').delegate('[type="button"]', 'click', remove_item);
    

    where the buttons are part of the items class and remove_item is the callback for the removal.

提交回复
热议问题