由于该功能执行后如何禁用Enter / Return键?

| 我有此功能,其中#text_comment是文本区域的ID:
$(\'#text_comment\').live(\'keypress\',function (e) {

    if(e.keyCode == 13) {
        textbox = $(this);
        text_value = $(textbox).val();

        if(text_value.length > 0) {
            $(this).prev().append(\'<div id=\"user_commenst\">\'+text_value+\'</div>\');
            $(textbox).val(\"\");
        } 
    }                                               
});
发生的事情是,当按下回车键或回车键(键代码13)时,文本将追加,但是它也将文本向下移动一行,这是按回车键所应该的。 即使我将文本框的值设置为\“ \”,也会发生这种情况。     
已邀请:
        在这里回答http://jsfiddle.net/Z9KMb/     
        怎么样event.preventDefault()     
        进入“ 1”案例时,尝试停止事件传播(请参阅http://snipplr.com/view/19684/stop-event-propagations/)。     
        试试这个event.stopImmediatePropagation()
$(\'#text_comment\').live(\'keypress\',function (e) {       
          if(e.keyCode == 13) {   
               e.stopImmediatePropagation()
       ///rest of your code
  }
});
    
        我已经对此进行了测试,这可行。输入不会创建新行。
$(\'#text_comment\').live(\'keypress\',function (e) {

    if(e.keyCode == 13) {
        textbox = $(this);
        text_value = $(textbox).val();

        if(text_value.length > 0) {
            $(this).prev().append(\'<div id=\"user_commenst\">\'+text_value+\'</div>\');
            $(textbox).val(\"\");
        }
        return false;
    }                                               
});
尽管我想知道,如果您不想换行,为什么要使用textarea,为什么不使用输入type = \'text \'呢?     

要回复问题请先登录注册