繁体   English   中英

Javascript 功能不适用于 Internet Explorer

[英]Javascript function not working with Internet Explorer

这是我的 javascript 函数,用于防止用户在文本框字段中输入字母和特殊字符,它工作正常,但它甚至不接受数字。

请注意,它在 Firefox 中运行良好。

function isNumber(event) {
            var regex = new RegExp("^[0-9]+$");
            var str = String.fromCharCode(!event.charCode ? event.which : event.charCode);
            if (regex.test(str)) {
                return true;
            }
            if (window.event) {
                window.event.returnValue = false;
            }

            return false;
        }

这是我的文本框

<asp:TextBox Width="50px" ID="placesvis" runat="server" onkeypress="isNumber(event);"

为什么不简单地将输入类型设置为数字?

<input type="number" id="quantity" name="quantity" min="1" max="5">

isNumber 不是接收事件对象,而是接收文本框作为参数。

你可以试试这个: document.getElementById("placesvis").addEventListener('keypress', isNumber);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM