输入猛击事件两次 [英] Input Fires Keypress Event Twice

查看:88
本文介绍了输入猛击事件两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题之前已经被问过/回答过(大多数情况下),但我已经尝试了三件事来阻止事件冒泡但没有任何效果:

This question has been asked/answered (mostly) before, BUT I've tried three things to stop the event from bubbling but nothing has worked:

return false;
e.stopPropagation();
e.preventDefault();  

(返回false应该照顾其他两个,对吗?)

(return false should take care of the other two,correct?)

这是html:

<div class="tags-holder">
    <input type="text" class="addField" id="addField_<%= visit.id %>"  placeholder="add a new tag">
</div>

和JS(更新清理):

    $('.addField').show().keyup(function(event){
  event.preventDefault();

      if(event.keyCode == 13 || event.keyCode==9) {
    ProfilePage.createTag( this, 'nada', 'addField')
        $(this).hide().val('');

        return false;       
   }

} );

我把多余的塞子留在那里,但真的不应该返回假只是杀死冒泡? (使用Chrome)。

I left the redundant stoppers in there but really shouldn't return false simply kill the bubbling? (using Chrome).

线索? keyCode = 13是Enter

Clue? keyCode=13 is "Enter"

推荐答案

哇。你的帮助很棒,帮助我思考。

Wow. Your help was great and helped me think it through.

但解决方案感觉有点像警察出局;有效,但条件永远不应该在那里。

BUT the solution feels a bit like a cop-out; effective, but the condition should never be there in the first place.

这是我在这里的评论中找到的:
http://yuji.wordpress.com/2010/02/22/jquery-click-event-fires-twice/

Here it is, which I found in the comments from here: http://yuji.wordpress.com/2010/02/22/jquery-click-event-fires-twice/

    $('.plus').unbind('click').bind('click',function(e){    
console.log('clicked')
    var id=$(this).attr('plus_id');
    var field=$('<input type="text">').attr({'placeholder':'add a new tag','id': 'addField_' + id, 'visit_id':id});
    field.focus();
    field.show().keydown(function(event){
        event.stopImmediatePropagation();
        if(event.keyCode == 13 || event.keyCode==9) {
            console.log(event)
            ProfilePage.createTag( field, 'nada', 'addField')
            field.hide().val('');
            return false;       
        }
    }).click(function(e){
        return false;
    })
    ;
$(this).append(field);
return false;       
   });

这篇关于输入猛击事件两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆