gpt4 book ai didi

javascript - 如何忽略文本框中不需要的字符(JavaScript 或 jQuery)

转载 作者:搜寻专家 更新时间:2023-11-01 05:26:31 24 4
gpt4 key购买 nike

有一个文本框,我想让用户只输入数字而不是任何字母字符。那么我怎么能忽略最终用户通过 JavaScript 或 jQuery 输入的那些字符呢?注意我不想用空字符串替换用户输入的值;相反,如果有任何办法,我想忽略大小写。

最佳答案

试试这个代码:

$("#id").keypress(function (e) {
//if the letter is not digit then display error and don't type anything
if ( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) {
return false;
}
});

引用 http://roshanbh.com.np/2008/04/textbox-accept-only-numbers-digits.html

关于javascript - 如何忽略文本框中不需要的字符(JavaScript 或 jQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4450056/

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