这个问题以前已经有过几种不同的提问方式,但我无法在我的场景中使任何答案都起作用。
我正在使用jQuery实现命令历史记录,当用户按上下箭头时。当按上箭头时,我会将输入值替换为先前的命令,并设置输入字段的焦点,但希望光标始终位于输入字符串的末尾。
我的代码如下:
$(document).keydown(function(e) {
var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
var input = self.shell.find('input.current:last');
switch(key) {
case 38: // up
lastQuery = self.queries[self.historyCounter-1];
self.historyCounter--;
input.val(lastQuery).focus();
// and it continues on from there
如何在聚焦后强制将光标放置在 'input' 的末尾?
$('input[name=item1]').val(value).focus();
- inMILD