Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of jquery bind failure

Shulou Source: shulou.com Published: 2022-06-02 15:47:25 10月21日 Update

This article mainly shows you "how to solve the jquery bind failure problem", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to solve the jquery bind failure problem" this article.

The solution to jquery bind failure: 1, create a HTML sample file; 2, use "$(" .sentnum-box "). Delegate (" .a-add-ordergoods "," click ", function () {.});" method can be solved.

This article operating environment: windows7 system, jquery3.2.1 version, DELL G3 computer

How to solve the problem of jquery bind failure?

JQuery uses invalid handling methods for bind dynamic binding events

Recently, in page development, when doing page special effects, you need to assign an event to a dynamically loaded button.

So bind the event with $(obj) .bind (); without thinking.

But there is a problem:

Bind does bind events, but that's relative to fixed html tags.

When the page content is dynamically loaded, there is a bug for the bind event, which can only be bind once, but it is useless when you trigger the event the second time.

For example:

I assign a click to the tag, and the tag, including the content, is read from the background data and loaded dynamically. When I use bind to bind click events, the first click on click successfully triggers

But the second click, the click event is invalid.

Later research found that jQuery also has a way to bind events: delegate ()

The usage is as follows:

$(".sentnum-box") .delegate (".a-add-ordergoods", "click", function () {/ / js data code})

In this way, dynamic data binding events can be implemented and will never fail.

The above is all the contents of the article "how to solve the problem of jquery bind failure". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Events questions content dynamics data methods time tags articles pages learning help research success without thinking code methods background buttons files Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Apple Shulou Tech Info MySQL Linux