Javascript 回发后在文本框上设置光标位置
回发后,我可以使用以下代码关注文本框:Javascript 回发后在文本框上设置光标位置,javascript,asp.net,Javascript,Asp.net,回发后,我可以使用以下代码关注文本框: ScriptManager.RegisterStartupScript(textBox, textBox.GetType(), "selectAndFocus", "$get('" + textBox.ClientID + "').focus();", true); textBox.Attributes.Add("onfocus", "$get('" + textBox.ClientID + "').value = $get('" + textBox.C
ScriptManager.RegisterStartupScript(textBox, textBox.GetType(), "selectAndFocus", "$get('" + textBox.ClientID + "').focus();", true);
textBox.Attributes.Add("onfocus", "$get('" + textBox.ClientID + "').value = $get('" + textBox.ClientID + "').value;");
但这会将光标位置设置为文本框的开头,而不是最后键入的字符之后。我尝试使用以下代码来解决此问题:
ScriptManager.RegisterStartupScript(textBox, textBox.GetType(), "selectAndFocus", "$get('" + textBox.ClientID + "').focus();", true);
textBox.Attributes.Add("onfocus", "$get('" + textBox.ClientID + "').value = $get('" + textBox.ClientID + "').value;");
但这不起作用。结果和以前一样。
我怎样才能解决这个问题
我已经阅读了大量的链接,似乎是最好的解决方案,但我一直没能让它发挥作用
更新:忘记提到文本框位于updatepanel中
更新2,尝试的解决方案:
string setCaretTo = @"function setCaretTo(obj, pos) {
if(obj.createTextRange) {
/* Create a TextRange, set the internal pointer to
a specified position and show the cursor at this
position
*/
var range = obj.createTextRange();
range.move('character', pos);
range.select();
} else if(obj.selectionStart) {
/* Gecko is a little bit shorter on that. Simply
focus the element and set the selection to a
specified position
*/
obj.focus();
obj.setSelectionRange(pos, pos);
}
}";
ScriptManager.RegisterClientScriptBlock(//anotherunrelated script);
ScriptManager.RegisterClientScriptBlock(textBox, textBox.GetType(), "MyScript", setCaretTo, true);
ScriptManager.RegisterStartupScript(textBox, textBox.GetType(), "MyStartupScript", "window.onload = function() {obj = window.document.getElementById('"+textBox.ClientID+"');setCaretTo(obj, obj.getAttribute('value').length);}", true);`
使用您提供的链接中的
setCaretTo
函数,我添加了以下js:
window.onload = function() {
obj = window.document.getElementById('myTextBox');
setCaretTo(obj, obj.getAttribute('value').length);
}
这将设置为最后一个字符 使用您提供的链接中的
setCaretTo
函数,我添加了以下js:
window.onload = function() {
obj = window.document.getElementById('myTextBox');
setCaretTo(obj, obj.getAttribute('value').length);
}
这将设置为最后一个字符 帮我解决了这个问题:
string setCursorToEndScript = @"function SetCursorToTextEnd(textControlID)
{
var text = document.getElementById(textControlID);
if (text != null && text.value.length > 0)
{
if (text.createTextRange)
{
var FieldRange = text.createTextRange();
FieldRange.moveStart('character', text.value.length);
FieldRange.collapse();
FieldRange.select();
}
}
}
";
string setFocusScript = @"$get('" + textBox.ClientID + "').focus();";
ScriptManager.RegisterClientScriptBlock(textBox, textBox.GetType(), "SetCursorToTextEnd", setCursorToEndScript, true);
ScriptManager.RegisterStartupScript(textBox, textBox.GetType(), "selectAndFocus", setFocusScript, true);
textBox.Attributes.Add("onfocus", "SetCursorToTextEnd('"+ textBox.ClientID +"');");
为我解决了这个问题:
string setCursorToEndScript = @"function SetCursorToTextEnd(textControlID)
{
var text = document.getElementById(textControlID);
if (text != null && text.value.length > 0)
{
if (text.createTextRange)
{
var FieldRange = text.createTextRange();
FieldRange.moveStart('character', text.value.length);
FieldRange.collapse();
FieldRange.select();
}
}
}
";
string setFocusScript = @"$get('" + textBox.ClientID + "').focus();";
ScriptManager.RegisterClientScriptBlock(textBox, textBox.GetType(), "SetCursorToTextEnd", setCursorToEndScript, true);
ScriptManager.RegisterStartupScript(textBox, textBox.GetType(), "selectAndFocus", setFocusScript, true);
textBox.Attributes.Add("onfocus", "SetCursorToTextEnd('"+ textBox.ClientID +"');");
还是没用。用更多信息更新了原始帖子。仍然无法正常工作。更新了原始帖子的更多信息。selectandFocus功能在哪里?你能提供给我吗?选择和聚焦功能在哪里?你能给我提供一下吗?