Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JQuery:防止在执行特定操作时触发其他事件_Jquery_Event Handling - Fatal编程技术网

JQuery:防止在执行特定操作时触发其他事件

JQuery:防止在执行特定操作时触发其他事件,jquery,event-handling,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' f

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

让我们举一个例子:

   $(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 = '';
});

});
目的:防止在单击“重新加载验证码”时触发“更改”事件

$(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文档中尝试了unbind、stopPropagation和其他函数,但都不起作用

请注意,我只想在单击特定链接时冻结“更改”。在其他情况下,它应该一直工作


提前谢谢各位!我希望有人能为我澄清这一点。

除非单击
$(“#link_id”)
时触发的代码正在更改元素
$(“#username”)
,否则更改事件不应触发。。。但是任何一种方法都应该满足您的需要。

来自jQuery文档:

阻止更多的处理者 在一次绑定后使用 .live(),处理程序必须返回 错。调用.stopPropagation()将 我们不能做到这一点


您是否尝试过使用.live()而不是.bind()并返回false?

如果没有看到完整的代码,很难回答,但下面的代码可能会有所帮助。我知道这不是一个完美的解决方案,但可能会有所帮助

$("#username").bind('change', function () {
    if ( $('body').data('linkClicked') !== 1 )
    {
        // Validate field when element loses focus
    }
});

$("#link_id").bind('click', function () {
    $('body').data('linkClicked', 1);
    // Do something when the a specific link is clicked
});

以下是我的代码的新版本:

$(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 = '';
});

});
目的:防止在单击“重新加载验证码”时触发“更改”事件

$(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('');

    });

});
  • 演示:

PS:中的一个也很好。

#link_id
元素的单击处理程序中的代码,用于更改
#username
字段的值,这就是为什么会触发更改事件?只需寻找澄清,代码实际上在#link_id:document.getElementById('security_code')的点击处理程序中。value='';事实上,它确实改变了它。我使用刷新图像链接来清除安全代码字段。问题是:当我单击“刷新”时,将生成一个新的验证码,并清除“安全代码”字段,以便用户可以输入新代码。但是当它被清除时,change事件触发,它说代码不正确,看起来不专业。我本以为preventDefault()将只应用于bind()语句中的元素,而在本例中,他是跨两个单独的元素工作的?下面是代码的新版本:非常感谢您的回复!刚刚发布了一个新版本的代码,这样您就可以更好地理解我想要的内容。如果在单击事件中您将值重置为零,那么在更改事件中,如果值为零,您就不能告诉它跳过验证吗?请参阅我的第二个答案…正确,但看起来“更改”在“单击”之前触发,并且不起作用!它就是不起作用!看起来“更改”是在“单击”之前触发的!谢谢你们的回复,伙计们。我不敢相信这件看起来很小的事情会给我带来这么多麻烦。这可能很简单,但您是否尝试过将单击事件移动到代码中更改事件处理之上?