如何使用 JavaScript 和/或服务器端标签获取绑定控件的客户端 ID?

发布于 2024-07-24 23:50:23 字数 380 浏览 5 评论 0原文

<asp:DataGrid>
    <ItemTemplate>
        1)
        <asp:TextBox ID="tbComments" onChange="javascript:checkLength(<%# tbComments.ClientId %>);" runat="server"/>
        2)
        <span id="<%# tbComments.ClientId %>Label"></span>
    </ItemTemplate>
</asp:DataGrid>

有什么想法可以使上述工作正常进行吗(这不是:P)?

<asp:DataGrid>
    <ItemTemplate>
        1)
        <asp:TextBox ID="tbComments" onChange="javascript:checkLength(<%# tbComments.ClientId %>);" runat="server"/>
        2)
        <span id="<%# tbComments.ClientId %>Label"></span>
    </ItemTemplate>
</asp:DataGrid>

Any ideas to make the above working (which doesn't :P)?

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

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

发布评论

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

评论(4

你的背包 2024-07-31 23:50:24

您确定问题不仅仅是以下标签中“ClientId”的拼写错误吗? :-)

<span id="<%# tbComments.CliendId %>Label"></span>

Are you sure the problem isn't just the mispelling of "ClientId" in the following tag? :-)

<span id="<%# tbComments.CliendId %>Label"></span>
寒尘 2024-07-31 23:50:24

您的 JavaScript checkLength() 函数没有将 Label 附加到客户端 ID 是否有问题? 那么你的 Javascript 是否同时采用两者?:

<asp:DataGrid>
    <ItemTemplate>
        1)
        <asp:TextBox ID="tbComments"
            onChange="javascript:checkLength(<%# tbComments.ClientId %>, <%# tbComments.ClientId %>Label);"
            runat="server"/>
        2)
        <span id="<%# tbComments.ClientId %>Label"></span>
    </ItemTemplate>
</asp:DataGrid>

Is there something wrong with your JavaScript checkLength() function not appending Label to the Client ID? Then have your Javascript take both?:

<asp:DataGrid>
    <ItemTemplate>
        1)
        <asp:TextBox ID="tbComments"
            onChange="javascript:checkLength(<%# tbComments.ClientId %>, <%# tbComments.ClientId %>Label);"
            runat="server"/>
        2)
        <span id="<%# tbComments.ClientId %>Label"></span>
    </ItemTemplate>
</asp:DataGrid>
单调的奢华 2024-07-31 23:50:23

更改您的标记以将“this”作为给定评论框的引用传递。

<asp:TextBox ID="tbComments" 
           onChange="javascript:checkLength(this);" runat="server"/>

然后在 checkLength() 函数中,“e”是对引发 onchange 事件的 DOM 元素的直接引用。

function checkLength(e){
  alert(e.id); //id of the comments box
  //get a reference to the span element immediately after the textbox
  theSpan = e.parentNode.getElementsByTagName("span")[0];
  theSpan.innerHTML = "comments length: " + e.value.length;
}

Change your markup to pass "this" as a reference to the given comments box.

<asp:TextBox ID="tbComments" 
           onChange="javascript:checkLength(this);" runat="server"/>

Then in your checkLength() function, "e" is a direct reference to the DOM element that raised the onchange event.

function checkLength(e){
  alert(e.id); //id of the comments box
  //get a reference to the span element immediately after the textbox
  theSpan = e.parentNode.getElementsByTagName("span")[0];
  theSpan.innerHTML = "comments length: " + e.value.length;
}
☆獨立☆ 2024-07-31 23:50:23

只需向 span 或文本框添加一个类,然后使用 jQuery 在 dom 中查找元素,然后向文本框添加更改事件,运行 checkLength 代码

$(document).ready(function()
{
    $('.myClass').change(function()
    {
         // do your length check...
    });
});

Just add a class to the span or the textbox and use jQuery to find the element in the dom and add a change event to the textbox run the checkLength code

$(document).ready(function()
{
    $('.myClass').change(function()
    {
         // do your length check...
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文