Talking about the problem of jquery clicking label twice to trigger
- 2021-06-28 10:23:13
- OfStack
When I encountered an label click while writing the questionnaire today, the listening click event was executed twice;Does this happen for any reason?Event Bubbling
<div class="questionBox checkBox">
<div class="title"> 2. How do you understand the importance of innovation awareness ?</div>
<div class="checkBoxList" data-more="2">
<label>
<input type="checkbox" />
<span> Innovation consciousness is necessary for work </span></label>
<label>
<input type="checkbox" />
<span> Innovation consciousness is necessary for work </span></label>
<label>
<input type="checkbox" />
<span> Innovation consciousness is necessary for work </span></label>
<label>
<input type="checkbox" />
<span> Innovation consciousness is necessary for work </span></label>
<label>
<input type="checkbox" />
<span> Innovation consciousness is necessary for work </span></label>
</div>
<input type="text" class="text" placeholder=" Enter additional comments here " />
</div>
$(".checkBoxList label").click(function(){
var more = $(this).parent(".checkBoxList").attr("data-more");
var length = $(this).parent(".checkBoxList").find("label").length;
var NowCheck=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(" Maximum number of choices for this topic " + more + " individual ")
}
})
Then find the following method.
Method 1: Throw label away.
Then method 2
Recognize only input and determine that the source of the event is input (this is a solution posted by someone online)
/**
* Whether to include a id Of input Descendant element
* @param {Element} elm Elements to Judge
* @param {String} id To match id
* @return {Boolean}
*/
function hasInput(elm, id) {
for (var i = 0, inputs = elm.getElementsByTagName("input"), len = inputs.length; i < len; i++) {
if (inputs[i].id === id) {return true;}
}
return false;
}
/**
* Judging under an element label Is there an association input
* @param {Element} elm Elements to Judge
* @param {Element} label label element
* @return {Boolean}
*/
function isLabelhasRelativeInput(elm, label) {
if (label.getElementsByTagName("input").length) {
return true;
}
var forT = label.getAttribute("for");
var isIE6 = !-[1,] && !window.XMLHttpRequest;// IE6 I won't support it for attribute
if (forT && hasInput(elm, forT) && !isIE6) {
return true;
}
return false;
}
document.getElementById("test").onclick = function(e) {
var ev = e || window.event;
var srcElm = ev.target || ev.srcElement;
if (srcElm.tagName === 'LABEL' && isLabelhasRelativeInput(this, srcElm)) {return;}
// do something;
}
....Anyway, I'm watching a little drunk
Then method 3.
Judging from the time stamp of event triggering, the problems related to event bubbling can be handled by this method.Safe and harmless
var evTimeStamp = 0;
document.getElementById("test").onclick = function(e) {
var now = +new Date();
if (now - evTimeStamp < 100) {
return;
}
evTimeStamp = now;
console.log(2);
}
Good to finish the discussion.I threw label away.HOHOHO
$(".checkBoxList label input").click(function(){
var more = $(this).parents(".checkBoxList").attr("data-more");
var length = $(this).parents(".checkBoxList").find("label").length;
var NowCheck=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(" Maximum number of choices for this topic " + more + " individual ")
}
})