如何在输入数字的同时在数字输入字段中添加破折号? [英] How to add dashes into a number input field while entering the number?
问题描述
我正在尝试使用java脚本在输入时在每个 4th
数字中的html数字字段中插入破折号。我在on-blur而不是on-key -press,on-key-up等等。但是当我试图将功能更改为按键按下/ on-key-up
事件时,它没有给出预期的结果。
这是我使用的代码。
< html>
< head>
< script>
函数addDashes(f)
{
f.value = f.value.slice(0,4)+ - + f.value.slice(4,8)+ - + f.value.slice(8,12);
}
< / script>
< / head>
< body>
电话:< input type ='text'name ='phone'onblur ='addDashes(this)'maxlength ='12'>< BR>
< / body>
< / html>
我是'JavaScript'的初学者。我在哪里做错了?
这会起作用。它也支持'删除'号码。
但是,我建议你使用 masking
< script src =https://ajax.googleapis.com/ajax/libs/jquery/2.1。 1 / jquery.min.js>< / script>< input type ='text'id =txtPhoneNoname ='phone'maxlength ='14'>< BR>