简体   繁体   English

preventDefault是否正在停止传播?

[英]preventDefault is stopping propagation?

Using legacy .NET Web Forms, I have a scenario where the user wants to calculate values when pressing the enter key in a given textbox. 使用旧版.NET Web窗体,我遇到一种情况,用户要在给定文本框中按Enter键时要计算值。 This works fine, however, the enter keypress is also firing the default form submit button on the page. 效果很好,但是,Enter键也触发了页面上的默认表单提交按钮。

I have tried to get around it by adding event.preventDefault() and return false to the textbox's event handler, but the form is submitting prior to the event occurrence so this code has no affect. 我试图通过添加event.preventDefault()来解决该问题,并将return false到文本框的事件处理程序,但是该表单是在事件发生之前提交的,因此此代码无效。

$("input[type='text']").keypress(function (event) {
    if (event.keyCode == 13) {
        var regionId = $(this).attr("data-region-id");
        var subRegionId = $(this).attr("data-subregion-id");
        var multiplier = $(this).val();
        CalculateSales(regionId, subRegionId, multiplier);
        event.preventDefault();
        return false;
    }
});

After reading a bit more, I learned that I could attach an event handler to the default form and use preventDefault() to allow propagation of events: 阅读更多内容后,我了解到可以将事件处理程序附加到默认格式,并使用preventDefault()允许事件传播:

$("#form").on("keyup keypress", function(e) {
    var code = e.keyCode || e.which; 
    if (code  == 13) {               
        e.preventDefault();
    }
});

With this method, the form is not submitted, but the input keypress event is not handled. 使用此方法,不会提交表单,但是不会处理输入的keypress事件。 How can I ensure that the form is not submitted but also the input keypress calculates the values? 如何确保未提交表单,但输入按键也能计算值?

No, preventDefault is not stopping propagation. 不, preventDefault不会停止传播。 These two functions have quite different functionality. 这两个功能具有完全不同的功能。

stopPropagation stops the event from going up the event chain. stopPropagation阻止事件沿事件链向上移动。

preventDefault prevents the default action in the event inside which it is called. preventDefault防止在preventDefault它的事件中发生默认操作。 However, the event still propagates up the chain. 但是,事件沿链传播

return false is equivalent with calling both preventDefault() and stopPropagation() . return false等同于调用preventDefault() and stopPropagation() However calling the functions is the preferred way because you can call them at the start of the function. 但是,调用函数是首选方法,因为您可以在函数开始时调用它们。 return false can be obviously used only at the end, and and error would prevent it from running. return false显然只能在末尾使用,并且错误将阻止它运行。

This is why most of the time you end up using both these functions in conjunction. 这就是为什么大多数情况下您最终同时使用这两个功能的原因。

Such is your case here, you have to use both . 这是您的情况, 您必须同时使用两者

$("#form").on("keyup keypress", function(e) {
    var code = e.keyCode || e.which; 
    if (code  == 13) {               
        e.preventDefault();
        e.stopPropagation();
    }
});

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

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