如何使用focus();在asp.net中

本文关键字:asp net 何使用 focus | 更新日期: 2023-09-27 18:29:04

如何使用focus();用户按下回车键后,我需要将光标保持在同一个文本框中。此外,当页面第一次打开时,如何将光标保持在给定的文本框中?

请帮忙。我不太擅长编程。我一直忙于我的研究项目。

我试着使用focus();通过将输出放入重定向到变量。但进展并不顺利。

如何使用focus();在asp.net中

给你的文本框一个id:

<asp:TextBox runat="server" id="textbox" />

使用jQuery聚焦:

<script>
    $(function(){
        $("#textbox").focus();
    });
</script>

别忘了在你的页面中包括jQuery:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

检查此示例:

<asp:TextBox runat="server" ID="txt_Text1" />
<div id="other">
  Trigger the handler
</div>
<script>
$('#txt_Text1').focus(function() {
  alert('Handler for .focus() called.');
});
</script>

你还必须添加

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>