简体   繁体   English

从同一个点击事件中停止两个或多个点击事件

[英]Stop two or more click events from the same click event

I have created a button in jsp page and i have written two click functions for the same button, one with the tag and the other with the class/id of the button.我在 jsp 页面中创建了一个按钮,我为同一个按钮编写了两个点击函数,一个带有标签,另一个带有按钮的类/id。 Now, when i click on the button, both the click functions are getting called one by one.现在,当我点击按钮时,两个点击功能都被一个接一个地调用。 My requirement is that inside the first click function, i want to stop the second click function. I do not want to remove the remove the second click function as in some cases i need it to execute.我的要求是在第一次点击 function 中,我想停止第二次点击 function。我不想删除第二次点击 function,因为在某些情况下我需要它来执行。

Below is the code i'm trying.下面是我正在尝试的代码。

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){

$("input[type=button]").click(function(e){

if(true) {
alert("The paragraph was clicked.");
}
else {
// I want to remove the second click function here
}
});

$("#test").click(function(){
   alert("Test paragraph was clicked.");

  });

});
</script>
</head>
<body>
<input type = 'button' value='test' id='test'/>
</body>
</html>

You need to use e.stopImmediatePropagation() :您需要使用e.stopImmediatePropagation()

$("input[type=button]").click(function(evt){
    evt.stopImmediatePropagation();
    ....
});

This prevents "sideways bubbling", for want of a better phrase, ie further event callbacks bound to the same element from firing.这可以防止“横向冒泡”,因为需要更好的短语,即绑定到同一元素的更多事件回调不会触发。

You should try to put你应该试着把

return false;

in the else statement.else语句中。 Since it stops the propagation of the event it should do what you want.因为它停止了事件的传播,所以它应该做你想做的。

Description: Attach a handler to an event for the elements.说明:将处理程序附加到元素的事件。 The handler is executed at most once per element.每个元素最多执行一次处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM