最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

I need Javascript function to disable enter key - Stack Overflow

programmeradmin3浏览0评论

I need to use JS function to disable enter key. I'm currently facing an issue with my Spring form

<form:textarea path="name" onkeypress="return noenter()"

here is the function I currently using

<script> function noenter() {   alert("Testing");
    return !(window.event && window.event.keyCode == 13); 
    } </script>

for some reason, the alert is working when I press on Enter key, but still facing same exception

HTTP Status 415 -

type Status report

message

description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (). Apache Tomcat/6.0.29

I need to use JS function to disable enter key. I'm currently facing an issue with my Spring form

<form:textarea path="name" onkeypress="return noenter()"

here is the function I currently using

<script> function noenter() {   alert("Testing");
    return !(window.event && window.event.keyCode == 13); 
    } </script>

for some reason, the alert is working when I press on Enter key, but still facing same exception

HTTP Status 415 -

type Status report

message

description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (). Apache Tomcat/6.0.29

Share Improve this question edited May 16, 2011 at 12:45 Sean Patrick Floyd 299k71 gold badges475 silver badges595 bronze badges asked May 16, 2011 at 12:26 Ali Taha Ali MahboubAli Taha Ali Mahboub 3,5116 gold badges28 silver badges25 bronze badges 2
  • window.event is not cross-browser I think. It's an IE thing... – Šime Vidas Commented May 16, 2011 at 12:29
  • Is there something peculiar about the textareas you are using? Typically, an HTML textarea element turns ENTER into newlines, rather than submitting the form, no javascript needed. – Doug Kavendek Commented May 16, 2011 at 17:56
Add a ment  | 

3 Answers 3

Reset to default 10

This should work:

Markup:

<form:textarea path="name" onkeypress="return noenter(event)">

JavaScript:

function noenter(e) {
    e = e || window.event;
    var key = e.keyCode || e.charCode;
    return key !== 13; 
}

Live demo: http://jsfiddle/Fj3Mh/

you need to address this in the keyup and keydown event. The browser uses the enter key independent of the actual web page. So you will need to stop event propagation at the keyup or keydown event. By the time the keypress event has been emitted the browser itself has received it and there is no way to keep it from processing. This is specific to the enter key and a few others. Character keys such as 'a' and 'b' do not suffer from this problem.

<script type="text/javascript"> 

function stopRKey(evt) { 
  var evt = (evt) ? evt : ((event) ? event : null); 
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;} 
} 

document.onkeypress = stopRKey; 

</script>
发布评论

评论列表(0)

  1. 暂无评论