2010-11-04 112 views
1

嘿, 在ASP.NET中使用简单的选项可以将焦点置于文本框元素中的文本末尾吗?没有JavaScript?在文本框的文本末尾设置焦点

+2

你需要JavaScript来做这个。 – ceejayoz 2010-11-04 12:54:12

+0

此连杆具有正确溶液 [设置焦点到文本的结束在回发后的文本框] [1] [1]:http://stackoverflow.com/questions/4032888/set-focus -to-结束文本中,文本框,后回传 – PSR 2013-03-25 09:40:51

回答

5

ASP.NET文本框呈现为标准的HTML输入(类型=“文本”),并达到你所要求的唯一途径是通过JavaScript:

var textbox = document.getElementById('foo'); 
textbox.focus(); 
textbox.value = textbox.value; 

其中foo是生成的ID输入:

<input type="text" id="foo" name="foo" value="some text" /> 
2

你可以使用这个服务中端:

ScriptManager.RegisterStartupScript(this, this.GetType(), "tmp2", "var t2 =  document.getElementById('foo'); t2.focus();t2.value = t2.value;", true); 
2

解决方案使用jQuery

$('#loginBtn').on('click',function(){ 
    var val = $('#txtLoginName').val(); 
    $('#txtLoginName').val(''); 
    $('#txtLoginName').val(val); 
    $('#txtLoginName').focus(); 
}); 

HTML代码

<input type="text" id="txtLoginName" value="test"/> 

Fiddle Example

0

这甚至适用于更新面板,我相信所有其他情况太

ScriptManager.RegisterStartupScript(this, this.GetType(), "tmp2", "$('#ContentPlaceHolder1_TxtSupplier').focus();var value = $('#ContentPlaceHolder1_TxtSupplier').val();$('#ContentPlaceHolder1_TxtSupplier').val('');$('#ContentPlaceHolder1_TxtSupplier').val(value);", true);