javascript textbox call event when value changes

后端 未结 3 1468
予麋鹿
予麋鹿 2021-01-06 08:58

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, b

相关标签:
3条回答
  • 2021-01-06 09:52
        <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 讨论(0)
  • 2021-01-06 09:55

    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();
    }
    
    0 讨论(0)
  • 2021-01-06 09:55

    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

    0 讨论(0)
提交回复
热议问题