Quick Solution for repeated jquery event binding

Source: Internet
Author: User

One $. fn. live repeat binding

Solution:Use the die () method to unbind all events bound to the element before binding the live () method, and then bind new events using the live () method.
Copy codeThe Code is as follows:
// Unbind from the die () method and then use live () Binding
$ ("# SelectAll"). die (). live ("click", function (){
// Event Running code
});

Second click and other events

Solution:The unbind ("click") method is used to unbind the Bound event and then bind the new event. That is, the original event on the object is removed before the event is bound to the object.

Complete Test code:
Copy codeThe Code is as follows:
<Div class = "box">
<Button id = "test"> repeat binding trigger button </button> (click this button twice or more to trigger repeat binding, and then click the following button to see the result)
<Br/>
<Button id = "test1"> click to bind the test button again </button>
<Button id = "test2"> click to bind a test button </button>
<Button id = "test3"> repeat the test button in live </button>
<Button id = "test4"> live binds a test button </button>
</Div>
<Script type = "text/javascript" src = "../static/jquery-1.6.1.min.js"> </script>
<Script type = "text/javascript">
$ (Function (){
Var I = 1, j = 1, k = 1, h = 1, n = 1;
Var triggerBind = function (){
$ ("# Test1"). click (function (){
Alert ("click is not unbound and rebinding is executed for" + j ++ "times ");
});
$ ("# Test2"). unbind ('click'). click (function (){
Alert ("click unbind execution" + k ++ "times ");
});

$ ("# Test3"). live ("click", function (){
Alert ("live is not unbound and repeat execution No." + h ++ "times ");
});
$ ("# Test4"). die (). live ("click", function (){
Alert ("executed after live is unbound" + n ++ "times ");
});
}
$ ("# Test"). click (function (){
TriggerBind ();
Alert ("trigger binding click" + I ++ "times ");
});
});
</Script>

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.