js 退格键并计算有多少个数字
我有这个代码用于计算输入的数字数
var tnnod=0;
function telephone(e) {
if (tnnod<10) {
var key;
var keychar;
if (window.event) {
key = window.event.keyCode;
}
else if (e) {
key = e.which;
}
else {
return true;
}
keychar = String.fromCharCode(key);
if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ) {
return true;
}
else if ((("0123456789").indexOf(keychar) > -1)) {
tnnod+=1;
return true;
}
else if (keychar == "-") {
return true;
}
else
return false;
}
else
return false
}
,但是每次按退格键时如何从计数器中删除 1 并且删除的字符是数字而不是“-”
我尝试过将 key == 8 获取到做某事,但由于某种原因按退格键并没有真正返回任何内容,
这可能是什么问题?
i have this code for counting how many digits where entered
var tnnod=0;
function telephone(e) {
if (tnnod<10) {
var key;
var keychar;
if (window.event) {
key = window.event.keyCode;
}
else if (e) {
key = e.which;
}
else {
return true;
}
keychar = String.fromCharCode(key);
if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ) {
return true;
}
else if ((("0123456789").indexOf(keychar) > -1)) {
tnnod+=1;
return true;
}
else if (keychar == "-") {
return true;
}
else
return false;
}
else
return false
}
but how do i remove 1 from the counter each time the backspace was hitted and the char that was deleted was a digit and not "-"
i have tried getting the key == 8 to do something but hitting the backspace doesn't really return anything for some reason
what can be the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不必专门检测退格键。试试这个:
这里的基本区别在于,不是每次按键时加 1,而是将
tn_count
更新为字段中所有数字字符的总数。为了安全起见,您可能可以进行更多清理,但这应该可以帮助您开始。You don't have to detect specifically the backspace keypress. Try this:
The basic difference here is that instead of adding 1 every time the key is pressed, just updated
tn_count
to be the total count of all digit characters in the field. You can probably do some more cleanup just to be safe, but this should get you started.我认为为类似的事情计算击键次数是一个坏主意。我们正在讨论文本字段的输入,对吧?如果用户从剪贴板粘贴某些字符串,您会怎么做?如果他用鼠标标记一些文本并将其删除怎么办?用一个字符替换它?
我认为仅查看文本字段中的文本并(如有必要)进行一些摆弄以确保语法正确会更有意义。让用户输入他想要的任何内容,如果您发现垃圾字符,您可以将文本替换为不包含这些字符的文本。此时您还可以修剪字段(没有前导或尾随空格)。此外,准确跟踪长度变得就像询问从字段返回的字符串的长度一样简单。
I think it's a bad idea to count keystrokes for something like that. We're talking about input into a text field, right? What will you do if the user does a paste of some string from the clipboard? What if he uses the mouse to mark some text and delete it? Replace it with one character?
I think it would make a lot more sense to just look at the text from the text field and (if necessary) do some fiddling to ensure proper syntax. Let the user enter whatever he wants, if you find garbage characters you can just replace the text with one that doesn't have those characters. You can also trim the field at this time (no leading or trailing spaces). Also, keeping accurate track of the length becomes as easy as asking for the length of the string returned from the field.
一些想法 -
A few thoughts -
您可以计算字符串的长度并使用该值吗?像下面这样:
Could you just count the length of the string and use that value? Something like the following: