Home > Web Front-end > JS Tutorial > How to deal with repeated binding when JS is dynamically loaded

How to deal with repeated binding when JS is dynamically loaded

php中世界最好的语言
Release: 2018-04-08 15:12:41
Original
1784 people have browsed it

This time I will show you how to deal with repeated binding when JS is dynamically loaded. What are the precautions when dealing with repeated binding when JS is dynamically loaded. The following is a practical case, let's take a look.

Preface

When adding a piece of data, dynamic loading is used to display it on the interface. Later, a serious bug was discovered. Take this thing I made. Take notes as an example. When I add a piece of data and then click Delete, I am prompted whether to delete it, as shown below:

But when I add two When more than one piece of data is deleted, you will be prompted several times to confirm the deletion.

After investigation, I finally found the problem.

Text

When content is added dynamically, the events that need to be used in the added p are usually written, such as click events/ Chang events, etc. Take my deletion event as an example. Since the deletion event must be written under the event of adding a note, when the first piece of data is added, it is bound once, when the second piece of data is added, it is bound once, and when the nth piece of data is added, it is bound once. , n times of deletion events have been bound, so when deleting the nth piece of data, you will be prompted n times to confirm the deletion.

Now that the cause of the error has been found, it can be solved. Before binding the delete event, just unbind the last bound event.

Code:

//解绑
$(".deletebtn").off("click");
$(".update").off("change");
//绑定
$(".deletebtn").bind('click', delete_click);
$(".update").bind('change', change_fonts);
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to learn vue for beginners

How to call child components from Angular parent components

What is the difference between wx:for and wx:for-item in WeChat mini program

The above is the detailed content of How to deal with repeated binding when JS is dynamically loaded. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template