访问不同 TD 中控件的 ClientID
我正在尝试从另一个控件中的 javascript(使用 jquery)调用访问一个控件中的 ClientID,以便执行一些客户端显示和隐藏效果。这是基本结构:
<td>
<asp:LinkButton OnClientClick="DoStuff" runat="server">
</td>
<td>
<asp:TextBox ID="blah" runat="server">
</td>
在脚本中:
function DoStuff() {
$("#<%= blah.ClientID %>").hide();
}
我遇到的问题是该函数不执行任何操作。它可以很好地隐藏同一 TD 中的对象,但在这里它似乎无法找到控件。
I am trying to access the ClientID of a control in one from a javascript (using jquery) call in a control in another in order to do some client-side show and hide effects. Here's the basic structure:
<td>
<asp:LinkButton OnClientClick="DoStuff" runat="server">
</td>
<td>
<asp:TextBox ID="blah" runat="server">
</td>
In Scripts:
function DoStuff() {
$("#<%= blah.ClientID %>").hide();
}
The problem I'm having, is that the function does nothing. It works fine hiding objects in the same TD, but here it doesn't seem to be able to find the control.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您直接引用服务器端的控件,
ClientID
应包含任何 NamingContainer 信息(来自 GridView 或 Repeater 等)。使用浏览器的开发工具(IE 有开发工具、Firefox 的 Firebug 等)找出控件的客户端 ID,然后调试 JavaScript(使用相同的工具)并执行$("#theidyoujustfound" )
在 JavaScript 控制台中查看最终结果。您的
DoStuff()
方法中似乎还缺少双引号;您需要$("#<%= blah.ClientID %>").hide();
。我不确定这是必要的,但在 OnClientClient 函数中包含括号不会有什么坏处:If you're referring directly to the control on the server side,
ClientID
should including any NamingContainer information (from something like a GridView or a Repeater). Find out what the client ID of the control is using your browser's developer tools (IE has Developer Tools, Firebug for Firefox, etc.), then debug JavaScript (with the same tool) and do a$("#theidyoujustfound")
in the JavaScript console and see what you end up with.It also looks like there's a missing double quote in your
DoStuff()
method; you'll want$("#<%= blah.ClientID %>").hide();
. And I'm not sure it's necessary, but it couldn't hurt to include the parenthesis for the OnClientClient function:要验证它找不到它,请尝试以下操作:
alert($("#<%= blah.ClientID %>).length);
如果它返回一个大于零的数字,则问题不在于查找。表格行是否位于
控件中?如果是,那么您也需要将脚本嵌入到转发器模板中。HTH。
To verify it can't find it, try this:
alert($("#<%= blah.ClientID %>).length);
If it returns a number greater than zero, then the issue isn't the find. Is the table row in a control like the<asp:Repeater />
control? If so, then you need to embed the script within the repeater template too.HTH.