0

所以假设我有一个看起来像这样的输入框:

<input id="fuzzyname" value="" placeholder="&Fuzzybear">

我想使用 javascript/JQuery 以便当我单击该输入框时,它会将光标放在 & 之后,并且占位符成为输入框中的值。

这是我到目前为止所拥有的:

$("#fuzzyname").focus(function () {
       var value = $(this).attr("placeholder");
       var symbol = "&";
       var cursorIndex = value.indexOf(symbol);
       //This is where I want to output the value into the inputbox with the cursor after cursorIndex 
 });
4

1 回答 1

1
var cursorIndex = value.indexOf(symbol) + 1;
$(this).val(value);
this.setSelectionRange(cursorIndex, cursorIndex);
于 2013-09-27T22:32:17.180 回答