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

javascript - OnKeyUp,OnKeyDown and onKeyPress - Stack Overflow

programmeradmin2浏览0评论

I try to use all here in this bination:

<SCRIPT LANGUAGE="JavaScript">

 function CountLeft(field, count, max) 
 {
     if (field.value.length > max)
         field.value = field.value.substring(0, max);
     else
         count.value = max - field.value.length;
 }

</SCRIPT>
<input name="text" onKeyDown="CountLeft(this.form.text, this.form.left,50);"
                   onKeyUp="CountLeft(this.form.text,this.form.left,50);"  onKeyPress="return entsub(event)">


<input readonly type="text" name="left" size=3 maxlength=3 value="50">  
characters left

But the enter key to submit does not work here, can anyone tell me how I can fix that? Oh and, I am trying to make a counter here.

Thanks!

I try to use all here in this bination:

<SCRIPT LANGUAGE="JavaScript">

 function CountLeft(field, count, max) 
 {
     if (field.value.length > max)
         field.value = field.value.substring(0, max);
     else
         count.value = max - field.value.length;
 }

</SCRIPT>
<input name="text" onKeyDown="CountLeft(this.form.text, this.form.left,50);"
                   onKeyUp="CountLeft(this.form.text,this.form.left,50);"  onKeyPress="return entsub(event)">


<input readonly type="text" name="left" size=3 maxlength=3 value="50">  
characters left

But the enter key to submit does not work here, can anyone tell me how I can fix that? Oh and, I am trying to make a counter here.

Thanks!

Share Improve this question edited Mar 26, 2011 at 14:44 Deniz Zoeteman asked Mar 26, 2011 at 14:09 Deniz ZoetemanDeniz Zoeteman 10.1k26 gold badges72 silver badges98 bronze badges 3
  • 1 You don't have a form, so nothing can be submitted really. – pimvdb Commented Mar 26, 2011 at 14:12
  • there is an actual form, just not included in the code i provided here, didn't feel that was necessary. – Deniz Zoeteman Commented Mar 26, 2011 at 14:16
  • try replacing "this.form.text" with 'this' only onKeyDown and onKeyUp – ahmedsafan86 Commented Mar 26, 2011 at 14:20
Add a ment  | 

2 Answers 2

Reset to default 3

I needed that script too and the one given by Kooilnc didn't really work for me. I used this:

function keyhandler(obj,e,max){
e = e || event;
max = max || 140;
var keycode = e.keyCode
  , len     = 0
  , This    = keyhandler
  , currlen = obj.value.length;

if (!('countfld' in This)){
    This.countfld = document.getElementById('letter-count');
}

if (keycode === 13) {
    //return document.forms[0].submit();
    return true;
}
if (currlen >= max) {
    This.countfld.innerHTML = '0';
    return false;
}
This.countfld.innerHTML = (max - obj.value.length);
return true;

}

And in the HTML file I used this:

<input type="text" onkeyup="return keyhandler(this,event,140)">

I hope it works for you! :D

Actually, you don't need all these keyhandlers. One keydown handler would be sufficient.

The here given function keyhandler stops updating the text input value after max is reached and submits the form if the key pressed was enter. You can find an example @ http://jsfiddle/KooiInc/2hrt7/.

<input type="text" onkeydown="return keyhandler(this,event,50)"/>

now keyhandler looks like this:

function keyhandler(obj,e,max) {
    e = e || event;
    max = max || 50;
    console.log(e.keyCode);
    if (e.keyCode === 13) {
        return document.forms[0].submit();
    }
    if (obj.value.length >= max && e.keyCode>46) {
        return false;
    }
    return true;
}

By the way, you are aware of the maxlength attribute of a text input field?

发布评论

评论列表(0)

  1. 暂无评论