开发者

Stopping enter key submitting also stops enter key in multi line text edit

There are lots of solutions on the web for stopping the enter key from submitting a form. Most commonly to use <body开发者_开发百科 onkeypress = ...

But these seem to have the undesired side effect of stopping the enter key working in a multi-line text box. Does anyone know of a way around this, so the enter key will still work in a multi-line text box?

Thanks,

AJ


<script language="Javascript">
    document.onkeydown = function() {
        if (event.keyCode == 13) {
            if (document.activeElement.tagName.toLowerCase () != "textarea") {
                      event.preventDefault();
                      return false;
                }
        }
    }
</script>

Something like this?


You can try the following (with jquery):

$(function(){
$('input:not(textarea)').live('keypress', function(e) {
        if (e.which == 13) return false;
        if (e.which == 13) e.preventDefault();
    });
});

Only input fields are targeted, not textareas.

NON JQUERY

 function disableEnterKey(e)
     {
          var key;      
          if(window.event)
               key = window.event.keyCode;
          else
               key = e.which;     

          return (key != 13);
     }

And add onKeyPress on all text inputs

<input type="text" name="textIn" onKeyPress="return disableEnterKey(event)"/>

Ref : http://www.bloggingdeveloper.com/post/Disable-Form-Submit-on-Enter-Key-Press.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜