Mar*_*ark 31 html javascript css jquery
我有以下函数将选择器添加到搜索输入作为高级选项,就像堆栈溢出高级搜索一样.
当您单击要搜索的内容时,它会添加前缀.见下面的Jquery:
<script>
$(document).ready(function () {
$("table#advanced_search_options tr").click(function () {
var SearchSelection = $(this).children("td:last-of-type").html();
var SearchInput = $('#Search');
SearchInput.val(SearchInput.val() + SearchSelection);
return false;
alert(SearchSelection);
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
如何操作上面的内容也将焦点带到#search输入,将胡萝卜(闪烁的文本插入光标)放在插入的文本/值的末尾?例如.
HC:< - 我的搜索输入的附加值,我想在这里设置光标,紧跟在:
pal*_*aѕн 122
您可以使用Input.setSelectionRangeRange API的一部分来与文本选择和文本光标进行交互:
var searchInput = $('#Search');
// Multiply by 2 to ensure the cursor always ends up at the end;
// Opera sometimes sees a carriage return as 2 characters.
var strLength = searchInput.val().length * 2;
searchInput.focus();
searchInput[0].setSelectionRange(strLength, strLength);
Run Code Online (Sandbox Code Playgroud)
演示:小提琴