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

Javascript textbox call event when value changes - Stack Overflow

programmeradmin0浏览0评论

I have a textbox, and whenever the value of the box changes, I want to check and see if 20 digits have been entered.

I thought that I would use the onChange event, but this seems to be interpreted as the onBlur event on IE.

So then I thought I would use onKeyDown, but the problem comes in if the user wants to paste a value into the field, then the function never gets called.

There are no other form boxes so I can't rely on onBlur or expect that they will change focus ever.

How do I do this?

I just want to evaluate the value of the textbox every time the value of the textbox changes.

<input type="text" onKeyDown="myfunction()">

I have a textbox, and whenever the value of the box changes, I want to check and see if 20 digits have been entered.

I thought that I would use the onChange event, but this seems to be interpreted as the onBlur event on IE.

So then I thought I would use onKeyDown, but the problem comes in if the user wants to paste a value into the field, then the function never gets called.

There are no other form boxes so I can't rely on onBlur or expect that they will change focus ever.

How do I do this?

I just want to evaluate the value of the textbox every time the value of the textbox changes.

<input type="text" onKeyDown="myfunction()">
Share Improve this question edited Sep 19, 2021 at 22:25 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 13, 2010 at 19:02 Senica GonzalezSenica Gonzalez 8,18216 gold badges69 silver badges111 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 10

An effective way is to use a combination of onkeydown and onpaste, which will work in IE, Firefox, Chrome and Safari. Opera is the odd one out here because it doesn't support onpaste so you may have to fall back to onchange for Opera or use the DOMAttrModified mutation event, see example below.

Internet Explorer also supports onpropertychange, which will fire every time a property changes on an element -- including value. I have read that DOMAttrModified is supported by Opera and Safari (not sure about Firefox), so you could use that in combination with IE's onpropertychange (untested):

if ("implementation" in document &&
                   document.implementation.hasFeature('MutationEvents','2.0'))
    myInput.addEventListener("DOMAttrModified", valueChanged, false);    
else if ("onpropertychange" in myInput)
    myInput.onpropertychange = valueChanged;

function valueChanged (evt)
{
    var attr;
    evt = evt || window.event;
    attr = evt.attrName || evt.propertyName;

    if (attr == "value")
        validate();
}

use onKeryPress event

 <input type="text" onKeyDown="CountText(this,200)" onpaste="return false;"> 

    function CountText(field, maxlimit) 
    {
       if (field.value.length < maxlimit) // if too long...trim it!
        {
            return true;
        }
        else
          return false;
    }

check my full article on : http://www.codeproject.com/KB/validation/MyTextBox.aspx

    <script>

function multy(val)
{
alert(val.value+"--"+val.id);
}

</script>
</head>
<body>
<input id="txtdemo" type="text" onchange="multy(this);"></br>
<input id="txtdemo" type="text" onchange="multy(this);">
</input>
</body>

Thanks... :)

发布评论

评论列表(0)

  1. 暂无评论