Today, when I was writing a questionnaire, when I encountered a label click, the Click event was executed two times; Event bubbling
<Divclass= "Questionbox CheckBox"> <Divclass= "title">2. How do you understand the importance of innovation awareness?</Div> <Divclass= "CheckBoxList"Data-more= "2"> <label> <inputtype= "checkbox" /> <span>Innovation consciousness is necessary for work</span></label> <label> <inputtype= "checkbox" /> <span>Innovation consciousness is necessary for work</span></label> <label> <inputtype= "checkbox" /> <span>Innovation consciousness is necessary for work</span></label> <label> <inputtype= "checkbox" /> <span>Innovation consciousness is necessary for work</span></label> <label> <inputtype= "checkbox" /> <span>Innovation consciousness is necessary for work</span></label> </Div> <inputtype= "text"class= "text"placeholder= "Enter a different opinion here" /> </Div>
$ (". CheckBoxList label"). Click (function(){ varMore = $ ( This). Parent (". CheckBoxList"). attr ("Data-more"); varLength = $ ( This). Parent (". CheckBoxList"). Find ("label"). length; varNowcheck=0; for(i=0;i<length;i++){ if($( This). Parent (". CheckBoxList"). Find ("label"). EQ (i). Find ("input"). Prop ("checked") = =true) {Pass= "1"Nowcheck= Nowcheck+1 } } if(nowcheck>More ) {Alert ("Most selected" + More + "one") } })
Then find the next method ...
Method One: Throw the label away ...
Then method two
Only input, judging the source of the event is input (this is a solution posted on the Internet) http://www.cnblogs.com/feng524822/p/4084037.html
/** * Whether to include the input descendant element of an id * @param {element} Elm to determine the elements * @param {String} ID to match the ID * @return {Boole an}*/ functionHasinput (Elm, id) { for(vari = 0, inputs = elm.getelementsbytagname ("input"), Len = Inputs.length; i < Len; i++) { if(Inputs[i].id = = = ID) {return true;} } return false; } /** * Determine if the label under an element has an associated input * @param {element} Elm the element to be judged * @param {element} label label element * @ return {Boolean}*/ functionIslabelhasrelativeinput (Elm, label) {if(Label.getelementsbytagname ("Input"). length) {return true; } varForT = Label.getattribute ("for"); varIsIE6 =!-[1,] &&!window. XMLHttpRequest;//IE6 does not support the for property if(ForT && hasinput (Elm, ForT) &&!isIE6) { return true; } return false; } document.getElementById ("Test"). onclick =function(e) {varEV = e | |window.event; varSrcelm = Ev.target | |ev.srcelement; if(Srcelm.tagname = = = ' LABEL ' && islabelhasrelativeinput ( This, Srcelm)) {return;} //Do something;}
。。。 I'm a little drunk, anyway.
Then method three ...
Judging by the timestamp of the event trigger, the problem that is related to event bubbling can be handled by this method. Safety and pollution-free
1 var evtimestamp = 0; 2 function(e) {3 var now = +new Date (); 4 If (Now-evtimestamp <) {5 return; 6 }7 evtimestamp = Now ; 8 Console.log (2); 9 }
Okay, here's the final discussion. I'm still a label to throw away ... Hohoho
$ (". CheckBoxList label Input"). Click (function(){ varMore = $ ( This). Parents (". CheckBoxList"). attr ("Data-more").); varLength = $ ( This). Parents (". CheckBoxList"). Find ("label")). length; varNowcheck=0; for(i=0;i<length;i++){ if($( This). Parents (". CheckBoxList"). Find ("label"). EQ (i). Find ("input"). Prop ("checked") = =true) {Pass= "1"Nowcheck= Nowcheck+1 } } if(nowcheck>More ) {Alert ("Most selected" + More + "one") } })
For a long time did not come out bubbling up ~ ~ ~ recently like a sponge absorbing the knowledge of the water. React also got Fengfenghuohuo!!! Occasionally also guest to write some PHP interface. Make a mall ... Plus a chat room ... Well ... Continue to work,
Finally wish all the Dragon Boat Festival still in the Code of Procedure ape, career success
jquery clicks the label to trigger a 2-time problem