为什么我无法在此处的标记中添加我的文本框的 clientId 以便我可以使用 jQuery 找到它?

发布于 2024-09-06 18:26:17 字数 445 浏览 4 评论 0原文

简而言之,我试图为客户端单击事件传递一个文本框“txtPersonId”的客户端 ID,以便我可以遍历该文本框控件并将其 jQuery 包装器传递给 loadePerson() 函数。

这就是我在 aspx 标记中对其进行 decalre 的方式:

<asp:Button ID="btnSearch" runat="server" Text="ARA" OnClientClick="loadPerson(jQuery('#<%=txtPersonId.ClientID %>'))"/>

但是当我渲染它时,

<%=txtPersonId.ClientID %>

占位符保持原样,并且不会被渲染控件的客户端 ID 替换。

知道为什么会发生这种情况以及我应该如何克服它吗?

Simply I am trying to pass the client Id of one textBox 'txtPersonId' for the client-click event so that I can traverse to that textBox control and pass its jQuery wrapper to the loadePerson() function.

This is how I decalre it in the aspx mark-up:

<asp:Button ID="btnSearch" runat="server" Text="ARA" OnClientClick="loadPerson(jQuery('#<%=txtPersonId.ClientID %>'))"/>

But when I render it, the

<%=txtPersonId.ClientID %>

place holder stays as it is and it is not replaced with the rendered control's client Id.

Any idea why this happens and how should I overcome that?

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

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

发布评论

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

评论(2

怀中猫帐中妖 2024-09-13 18:26:17

当我遇到这样的问题时,我将整个表达式括在括号中并将结果构建为字符串。我认为这是因为解析器无法识别嵌入在字符串属性中的渲染块语法。

 <asp:Button ID="btnSearch" runat="server" Text="ARA"
      OnClientClick='<%= "loadPerson(jQuery(\"#" + txtPersonId.ClientID + "\"))" %>' />

我很早就开始将我的 javascript 与我的标记完全分开,所以我可能对确切的细节有点模糊。如果您想将 javascript 与标记分开,您可以在脚本块中添加具有显式或相对 DOM 引用的处理程序。使用匹配 id 的“ends with”选择器可以消除查找显式 id 的需要,尽管您也可以这样做 - 下面的示例显示了两种样式。

 <script type="text/javascript">
      $(function() {
          $('#' + '<%= btnSearch.ClientID %>').click( function() {
                loadPerson( $('input[id$=txtPersonId]') );
          });
      });
 </script>

When I've had issues like this I've resorted to wrapping the entire expression in the brackets and building the result as a string. I assume that it's because the parser doesn't recognize the render block syntax embedded inside the string property.

 <asp:Button ID="btnSearch" runat="server" Text="ARA"
      OnClientClick='<%= "loadPerson(jQuery(\"#" + txtPersonId.ClientID + "\"))" %>' />

I've long since moved on to keeping my javascript completely separate from my mark up, so I may be a little fuzzy on the exact details. If you wanted to separate your javascript from your mark up you could either add the handler with an explicit or relative DOM reference in a script block. Using the "ends with" selector matching on the id removes the need to find the explicit id, though you could also do that -- the example below shows both styles.

 <script type="text/javascript">
      $(function() {
          $('#' + '<%= btnSearch.ClientID %>').click( function() {
                loadPerson( $('input[id$=txtPersonId]') );
          });
      });
 </script>
说好的呢 2024-09-13 18:26:17

我不知道它对您的 LoadPerson 有何作用,但如果您想在 Jquery 中为您的点击事件编写一个函数,您可以编写如下内容:

'#'+ '<%=txtPersonId.ClientID %>'要解决您的txtbox ID,您可以对按钮执行相同的操作 $('#' + '<%=btnSearch.ClientID%>').click(function(){//click event process});

I don't know what it does your LoadPerson,but if you want write a function for your click event in Jquery you can make something like:

'#'+ '<%=txtPersonId.ClientID %>' to address your txtbox ID you can do the samething for your button $('#' + '<%=btnSearch.ClientID%>').click(function(){//click event process});

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