单击 asp 标签后如何将焦点设置在文本框和/或控件上?

发布于 2024-09-06 06:56:30 字数 134 浏览 1 评论 0原文

我想在单击 asp 标签后将焦点设置在文本框和/或控件上?有人可以向我解释一下如何做到这一点吗?类似于做一个

<label for="txtBoxID">Blah</label>

I would like to set the focus on a textbox and/or control after clicking an asp label? Can some explain to me how to do this? Similar to doing a

<label for="txtBoxID">Blah</label>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

人│生佛魔见 2024-09-13 06:56:30

您也可以执行此操作

<label for="<%=txtName.ClientID%>">Name:</label>
<asp:TextBox runat="server" ID="txtName"></asp:TextBox>

或在点 4. 上

<label for="txtName">Name: </label>
<asp:TextBox runat="server" ID="txtName" ClientIDMode="Static"></asp:TextBox>

执行此操作并避免使用 JavaScript。

You can also do this

<label for="<%=txtName.ClientID%>">Name:</label>
<asp:TextBox runat="server" ID="txtName"></asp:TextBox>

or on dot 4.

<label for="txtName">Name: </label>
<asp:TextBox runat="server" ID="txtName" ClientIDMode="Static"></asp:TextBox>

and avoid JavaScript.

后eg是否自 2024-09-13 06:56:30

这是编写和避免 JavaScript 的最佳方法

<p>

<asp:Label ID="lblName" runat="server" AssociatedControlID="txtFirstName" Text="First Name: " />

<asp:TextBox ID="txtFirstName" runat="server" />

</p>

This is the Best way to write and avoid javascript

<p>

<asp:Label ID="lblName" runat="server" AssociatedControlID="txtFirstName" Text="First Name: " />

<asp:TextBox ID="txtFirstName" runat="server" />

</p>
少女净妖师 2024-09-13 06:56:30

您可以使用 Javascript 或 jQuery 来完成此操作。

<label for="txtBoxID" onClientClick="SetMyFocus()">Blah</label>

<javascript>
function SetMyFocus()
{
    document.getElementById("MyTextBox").focus();
}
</javascript>

如果您需要在单击标签时在服务器端执行某些操作,则必须在后面的代码中处理相同的操作,然后设置客户端脚本在重新加载页面后启动。使用 RegisterStartupScript 进行同样的操作。

You can do it using Javascript or jQuery.

<label for="txtBoxID" onClientClick="SetMyFocus()">Blah</label>

<javascript>
function SetMyFocus()
{
    document.getElementById("MyTextBox").focus();
}
</javascript>

If you have a specific need of doing something in the server side on the click of the label, you shall have to handle the same in code behind and then set the client side script to fire up after reloading the page. Use RegisterStartupScript for the same.

没有心的人 2024-09-13 06:56:30

我假设您想完全在客户端完成以避免回发?

您可以使用 jQuery 来设置焦点。添加对 jQuery 库的脚本引用后,您可以在页面中使用以下 JavaScript:

$(document).ready(function() {
     $("#labelId").click(function() {
          $("*[id$='txtBoxID']").focus()
     });
});

“*[id$='txtBoxID']”选择器允许您选择文本框的 ASP.NET 服务器端 ID,而无需任何其他操作代码。基本上,它的意思是“选择 ID 以 txtBoxId 结尾的任何 DOM 元素”。

您可以使用以下 CDN 脚本参考将 jQuery 添加到您的页面:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

I'm assuming you want to do it completely on the client side to avoid a postback?

You can use jQuery to set focus. After adding a script reference to the jQuery library, you can use the following JavaScript in your page:

$(document).ready(function() {
     $("#labelId").click(function() {
          $("*[id$='txtBoxID']").focus()
     });
});

The "*[id$='txtBoxID']" selector allows you to select the ASP.NET server side ID of your textbox without any additional code. Basically, it's saying "select any DOM element whose ID ends with txtBoxId."

You can add jQuery to your page with the following CDN script reference:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
傲影 2024-09-13 06:56:30

使用 jQuery 的更通用的解决方案:

$(document).ready(function() {
     $("label[for]").click(function() {
          var inputid = '#' + $(this).attr('for');
          $(inputid).focus();
     });
});

只要正确定义 for 属性,就应该处理所有标签。

A more generalized solution using jQuery:

$(document).ready(function() {
     $("label[for]").click(function() {
          var inputid = '#' + $(this).attr('for');
          $(inputid).focus();
     });
});

Should handle all labels, as long as you correctly define the for attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文