繁体   English   中英

如何将输入框 cursor 放在文本末尾

[英]how to put input box cursor at end of text

我想在按钮单击时在输入字段中添加一个数字,并在文本末尾设置 cursor position。 每次单击按钮时都会发生这种情况。

<input id="input"></input>
<button onclick="addnumber()">add</button>
addnumber(){

}
<input id="input"></input>
<button onclick="addnumber()">add</button>
    
<script>
    function addnumber() {
        const myInput = document.getElementById('input');
        const textLength = myInput.value.length;

        myInput.focus();
        myInput.setSelectionRange(textLength, textLength);
    }
</script>

 let input = document.getElementById('input') function addnumber() { input.value=69 input.focus() }
 <input id="input"> <button onclick="addnumber()">add</button>

JS代码:

let input = document.getElementById('input')
function addnumber() {
  input.focus(); 
  var val = this.input.value; 
  input.value = ''; 
  input.value = val;
}

HTML 代码:

  <input id="input">
  <button onclick="addnumber()">add</button>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM