Javascript 按enter键后重复多次

Javascript 按enter键后重复多次,javascript,jquery,Javascript,Jquery,当我专注于输入字段并按enter键时,一切正常,但当我再次这样做时,它会重复两次或更多,这取决于我聚焦的次数 $(document).delegate(".changeValue","focus one",function(){ $(this).on("keydown", function (event) { $(this).parent().find('.logisticError').remove(); if(e

当我专注于输入字段并按enter键时,一切正常,但当我再次这样做时,它会重复两次或更多,这取决于我聚焦的次数

  $(document).delegate(".changeValue","focus one",function(){   

        $(this).on("keydown", function (event) {

            $(this).parent().find('.logisticError').remove();

            if(event.keyCode == 13){

                var amountVal = /^\d+(\.)\d{2}$/

                if (!$(this).val().match(amountVal)) {

                    $(this).parent().append("<span class='logisticError'>you have an error</span>");

                } else {

                    field = $(this).data('field');
                    updateOrder(field, $(this).val());
                    $(this).blur();

                }           
            }       
        });
    });
$(document).delegate(“.changeValue”,“焦点一”,function(){
$(此).on(“向下键”),函数(事件){
$(this.parent().find('.logisticError').remove();
如果(event.keyCode==13){
var amountVal=/^\d+(\)\d{2}$/
如果(!$(this).val().match(amountVal)){
$(this.parent().append(“您有一个错误”);
}否则{
field=$(this.data('field');
updateOrder(字段,$(this.val());
$(this.blur();
}           
}       
});
});

因为每次有人聚焦该字段时,您都在创建一个事件。
.changeValue
,所以第一次创建事件时,第二次它将创建另一个事件,与第一次相同

我建议您删除委托内容(顺便说一句,不推荐使用)并直接绑定documentready上的字段

jQuery(document).ready(function(){
    $('.changeValue').on("keydown", function (event) .....
});

因此绑定是一次性完成的,而不是每次用户聚焦字段时都考虑只使用
.on()
,因为它已经取代了
.delegate()
@technophobia thx,我切换到.on()thx。我也消除了注意力(我认为这是主要问题),现在一切都很好。