停止同一个按钮上的点击事件之一
Stop one of click events on the same button
我有一个按钮,上面有两个点击事件。
我想要做的是根据条件阻止该按钮上的两个或所有事件。
我尝试使用 event.preventDefault();
、event.stopPropagation();
和 return false;
,但没有任何效果符合我的需要。
/* I need to stop $('button') event if the if condition
inside $('#btn') event passed */
$('#btn').click(function(e){
var term = $('#term').val();
if(term != '') {
//e.preventDefault();
//e.stopPropagation();
return false;
alert(2);
} else {
// continue
}
});
$('button').click(function(e){
alert(1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="term" type="text" placeholder="type here ..">
<button type="button" id="btn">Check</button>
要跳出您的函数,请仅使用 return;
(而不是 return false;
)
此外,您不能在 return
之后设置任何代码,所以这里是:
$('#btn').click(function(e){
e.preventDefault();
var term = $.trim( $('#term').val() );
if(term !== "") {
alert("Value exists! I'm breaking any further operations.");
return; // "Exit" my function
// YOU CANNOT WRITE CODE AFTER A RETURN STATEMENT!
} else { // Else is actually not needed but it's cool to have it for clearance
alert("Value is empty.");
}
});
$('button').click(function(e){
alert(1); // You'll see this in any case.
});
扩展您的问题如何防止弹出警报(1)
$('#btn').click(function(e){
e.preventDefault();
this.term = $.trim( $('#term').val() );
if(this.term) {
alert("Value exists!");
} else {
alert("Value is empty.");
}
});
$('button').click(function(e){
if(!this.term) {
alert(1);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="term" type="text" placeholder="type here ..">
<button type="button" id="btn">Check</button>
我假设您想停止这两个事件。如果是这样,您应该在 $('button').click() 处理程序中添加子句。
首先,在 $('#btn').click() 处理程序中添加 属性 以检查事件是否应该停止:
if(term != '') {
//e.preventDefault();
//e.stopPropagation();
this.stop = 1;
...
然后检查一下:
if (this.getAttribute("id") !== "btn" || this.stop !==1) {
alert(1);
}
这里JSFiddle
我有一个按钮,上面有两个点击事件。 我想要做的是根据条件阻止该按钮上的两个或所有事件。
我尝试使用 event.preventDefault();
、event.stopPropagation();
和 return false;
,但没有任何效果符合我的需要。
/* I need to stop $('button') event if the if condition
inside $('#btn') event passed */
$('#btn').click(function(e){
var term = $('#term').val();
if(term != '') {
//e.preventDefault();
//e.stopPropagation();
return false;
alert(2);
} else {
// continue
}
});
$('button').click(function(e){
alert(1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="term" type="text" placeholder="type here ..">
<button type="button" id="btn">Check</button>
要跳出您的函数,请仅使用 return;
(而不是 return false;
)
此外,您不能在 return
之后设置任何代码,所以这里是:
$('#btn').click(function(e){
e.preventDefault();
var term = $.trim( $('#term').val() );
if(term !== "") {
alert("Value exists! I'm breaking any further operations.");
return; // "Exit" my function
// YOU CANNOT WRITE CODE AFTER A RETURN STATEMENT!
} else { // Else is actually not needed but it's cool to have it for clearance
alert("Value is empty.");
}
});
$('button').click(function(e){
alert(1); // You'll see this in any case.
});
扩展您的问题如何防止弹出警报(1)
$('#btn').click(function(e){
e.preventDefault();
this.term = $.trim( $('#term').val() );
if(this.term) {
alert("Value exists!");
} else {
alert("Value is empty.");
}
});
$('button').click(function(e){
if(!this.term) {
alert(1);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="term" type="text" placeholder="type here ..">
<button type="button" id="btn">Check</button>
我假设您想停止这两个事件。如果是这样,您应该在 $('button').click() 处理程序中添加子句。 首先,在 $('#btn').click() 处理程序中添加 属性 以检查事件是否应该停止:
if(term != '') {
//e.preventDefault();
//e.stopPropagation();
this.stop = 1;
...
然后检查一下:
if (this.getAttribute("id") !== "btn" || this.stop !==1) {
alert(1);
}
这里JSFiddle