Click event Recurrence binding problem in jquery

Source: Internet
Author: User

In recent projects, you have encountered such a problem:

Remove a product from your wishlist, 1. If the deletion is determined directly, the deletion succeeds, 2. If you cancel the deletion, click again to confirm the deletion, the problem will occur, and the test finds:

For a future element (an object with the class name Deletefav), in the Click event, if the function also contains a click event with an element (called a), the Click event of the A element appears repeatedly bound when the two delete button is clicked

$ ("Body"). On ("click", ". Deletefav", function () {//Click delete button
var id=$ (this). attr ("id");
$ (". Alertbtns"). Append ("<span class= ' Cancel ' style= ' margin-left:14px; ' >CANCEL</span> ");
$ (". Alertwindow. Tips"). HTML ("is sure you want-remove this Photo from the favorites?");
$ (". Alertwindow"). Show ();
$ (". Alertbtns. conf"). Click (Function (event) {

Alert ("Test"); //------A second click. deletefav will find pop-up two times test
Location.href=rootdomain + '/wishlists/delete/' +id;
});

});

Workaround, first unbind, then bind

$ ("Body"). On ("click", ". Deletefav", function () {//Click delete button
var id=$ (this). attr ("id");
$ (". Alertbtns"). Append ("<span class= ' Cancel ' style= ' margin-left:14px; ' >CANCEL</span> ");
$ (". Alertwindow. Tips"). HTML ("is sure you want-remove this Photo from the favorites?");
$ (". Alertwindow"). Show ();
  $ (". Alertbtns. conf"). Unbind ("click"). Click (Function (event) {
Location.href=rootdomain + '/wishlists/delete/' +id;
});

});

Click event Recurrence binding problem in jquery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.