Transfer from:
https://blog.csdn.net/w6990548/article/details/79388905
1. onkeyup = "value=value.replace(/[^\d]/g,'')"
Using the onkeyup event, there is a bug, that is, in the state of Chinese input method, after entering a Chinese character, you will directly enter the letter
. 2. onchange = "value=value.replace(/[^\d]/g,'')"
Using the onchange event, after inputting content, the result will only be obtained when the input loses focus. Can not respond when inputting
3. oninput = "value=value.replace(/[^\d]/g,'')"
Using oninput event, the above two problems are solved perfectly, and there are no other problems in the test.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>input</title>
</head>
<body>
<br/>只能输入纯数字的输入框onkeyup :<input type="text" name="" onkeyup = "value=value.replace(/[^\d]/g,'')">
<br/><br/>只能输入纯数字的输入框onchange :<input type="text" name="" onchange = "value=value.replace(/[^\d]/g,'')">
<br/><br/>只能输入纯数字的输入框oninput :<input type="text" name="" oninput = "value=value.replace(/[^\d]/g,'')">
</body>
</html>