gpt4 book ai didi

javascript - Internet Explorer 处理数字输入的解决方法

转载 作者:行者123 更新时间:2023-11-28 00:34:01 27 4
gpt4 key购买 nike

我在 Internet Explorer 中遇到数字输入字段的问题。通常,此字段使用浏览器的区域设置来确定如何处理逗号。例如,在美国,逗号被简单地禁止输入,而在其他国家,它们可能被用来表示小数点。但是,此功能在 IE 中失效,逗号始终被视为小数点。这记录在案 here但是我还没有找到解决这个问题的有效解决方案。任何尝试获取该值并在 Javascript 中修改它的尝试都会失败,因为该值已被浏览器修改。对此有任何已知的解决方案吗?

注意:这是一个有angular的Node项目。

最佳答案

您可以使用 JavaScript OnKeyPress 事件来检查按下的键以限制输入,然后将数字格式化为仅包含小数点,代码如下:

<script>
function handleKeyPress(e) {
var newValue = e.target.value + e.key;

if (
// It is not a number nor a control key?
isNaN(newValue) &&
e.which != 8 && // backspace
e.which != 17 && // ctrl
newValue[0] != '-' || // minus
// It is not a negative value?
newValue[0] == '-' &&
newValue[0] == ',' &&
newValue[0] == '。' &&
newValue[0] == ',' &&
isNaN(newValue.slice(1)))

e.preventDefault(); // Then don't write it!
}
</script>
<input type="number" onKeyPress="handleKeyPress(event);" />

截图如下:

enter image description here

[注意] 上面的代码示例在 IE 9+ 上运行良好。

关于javascript - Internet Explorer 处理数字输入的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57366443/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com