JQuery:采取特定操作时防止触发其他事件

标签 jquery event-handling

正如我在标题中提到的,我想防止当我触发另一个事件时触发另一个事件:

让我们举个例子:

   $(function() {

$("#security_code").bind('change', function () {
  // Do validation and show errors
});

$("#reloadCaptcha").bind('click', function () {
  // generate new captcha and clear 'secuirity code' field

 // clear the field so the user will enter the new generated security code
  document.getElementById('security_code').value = '';
});

});

目的:防止单击“#reloadCaptcha”时触发“change”事件。

当我单击链接(第二个事件)时,第一个事件也会触发(因为“更改”激活)。我希望在实际单击链接时停止所有“更改”事件。我希望我说得足够清楚。有人有什么想法吗?

是的,我尝试过 jQuery 文档中的 unbind、stopPropagation 和其他函数,但它不起作用。

请注意,我只想在单击该特定链接时卡住“更改”。在其他情况下,它应该始终有效。

提前谢谢大家!我希望有人可以帮我澄清这一点。

最佳答案

$(function() {

    $("#security_code").bind('change', function () {

        // If #security_code is empty do nothing
        if ($('#security_code').val() == '')
        {
            return;
        }

        // Otherwise validate...

    });

    $("#reloadCaptcha").bind('click', function () {

        // generate new captcha and clear 'secuirity code' field

        // clear the field so the user will enter the new generated security code
        $('#security_code').val('');

    });

});

关于JQuery:采取特定操作时防止触发其他事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5371982/

相关文章:

jquery - jquery中的正则表达式获取图像文件名

jquery - 动态创建的图像行不会触发点击事件

javascript - Jquery自动幻灯片排序问题

java - 如何判断 SWT Shell 是通过编程方式关闭还是由用户关闭?

c# - 多次调用 KeyPress/EditorAction 事件处理程序

javascript - react : Element is changing a controlled input of type text to be uncontrolled

linux - eventfd 的 OS X 替代品

javascript - SimpleModal:如何在模态窗口中提交后传递mailtolink

php - 使用 jquery 从其他选择值中填充选择

java - Spring 集成测试中 ContextRefreshedEvent 过早触发