Jquery event rebinding Solution

Source: Internet
Author: User

One $. FN. Live repeat binding

Solution: Use the die () method. Before binding the live () method, unbind all events previously bound to this element, and then use live () method to bind a new event.

 
//Unbind from the die () method and then use live () binding.
$ ("# Selectall"). Die (). Live ("click ",Function(){
//Event runningCode
});

Second click and other events

Solution: Use the unbind ("click") method to unbind the Bound event and then bind the new event. That is, remove the original event on the object before binding the event to the object.

Complete Test code:

   <  Div class =  "  Box  "  >  
< Button ID = " Test " > Repeat the binding trigger button < / Button> (click this button twice or more to trigger repeated binding. Then, click the button below to view the result)
< BR / > <Br / >
< Button ID = " Test1 " > Click to repeat the binding test button < / 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 from the rebinding. " + J ++ + " Times " );
});
$ ( " # Test2 " ). Unbind ( ' Click ' ). Click ( Function (){
Alert ( " Click unbind execution " + K ++ + " Times " );
});

$ ( " # Test3 " ). Live ( " Click " , Function (){
Alert ( " Live is not unbound and repeated execution " + 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>

Complete Test code:

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.