在文本框中输入值时,当我在输入值之间输入值时,光标会自动移动到值的末尾。
<!DOCTYPE html>
<html>
<body>
<script>
function truncate(x) {
if(x.value.length > 0)
{
x.value = x.value.replace(/^\s+/, '');
}
}
</script>
<input id="otherText" onkeyup="javascript:truncate(this);" maxlength="12" type="text"/>
</body>
</html>
最佳答案
<!DOCTYPE html>
<html>
<body>
<script>
function truncate(x){
var val = document.getElementById("otherText");
var inputTextValue = val.value;
if(inputTextValue.length>0)
{
val.value = '';
}
}
</script>
<input id="otherText" onkeyup="javascript:truncate(this);" maxlength="12" type="text"/> </body>
</html>
请注意,我不知道如何用特定的正则表达式替换字符串。但上面的代码只是替换为空字符串。您可以根据您的要求进行修改。
关于javascript - 使用javascript打字时如何避免光标向后移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15271600/