jQuery:找到子级并绑定到事件

发布于 2024-10-27 04:39:35 字数 1210 浏览 1 评论 0原文

我正在尝试绑定到 ASP.NET 从 asp:Label 呈现为 span 的内容的 change 事件。显然,容器为这些控件添加了诸如 ctl00_ 之类的前缀,但我不希望我的脚本依赖于不一定已知的标识符。

span 通过 AjaxControlToolkit PasswordStrength 控件进行更新,该控件使用 ASP.NET 标签的 ID 来显示基于输入控件的帮助消息它正在验证。

仔细研究一下,我想要的似乎是可能的,因为控件有自己的 div 容器 - 我找到了资源 此处这里这里表明我基本上可以查找对于有选择器的孩子,SO 答案看起来绝对有希望,所以我想出了以下内容,仅略有改变:

<div id="PasswordHelpContainer" class="passwordFailureNotification">
    <asp:Label runat="server" ID="PasswordHelpLabel"/>
</div>

$(document).ready(function () {
    $('#PasswordHelpContainer').children('span:first').change(function () {
        //
    });
});

不幸的是,我的努力没有成功;该事件只是没有触发。

我在这里做错了什么? 我是否将 JavascriptjQuery 混合在一起?

我知道第二个问题对某些人来说可能听起来很矛盾,但如果需要,我会自由地详细说明。

I'm trying to bind to the change event of what is rendered as a span by ASP.NET from an asp:Label. Obviously the container prefixes these controls with something such as ctl00_, but I don't want to have my script dependent on identifiers that aren't necessarily known.

The span is updated via the AjaxControlToolkit PasswordStrength control which uses the ID the an ASP.NET label to display its help messages base on an input control it is validating.

On looking into it a bit, it seems what I want is possible as the control has its own div container - I found resources here, here and here that indicate I can essentially look-up the child with a selector, the SO answer looked absolutely promising so I came up with the following which alters only slightly:

<div id="PasswordHelpContainer" class="passwordFailureNotification">
    <asp:Label runat="server" ID="PasswordHelpLabel"/>
</div>

$(document).ready(function () {
    $('#PasswordHelpContainer').children('span:first').change(function () {
        //
    });
});

Unfortunately there was no success in my efforts; the event just doesn't fire.

What am I doing wrong here?
And am I mixing Javascript and jQuery together?

I know the second question might sound paradoxical to some but I'll freely elaborate if needs be.

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

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

发布评论

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

评论(2

装迷糊 2024-11-03 04:39:35

来自 http://api.jquery.com/change/

更改< /code> 当元素的值发生变化时,事件被发送到元素。此事件仅限于 元素、

因此,您的事件永远不会被引发,因为标签呈现为

From http://api.jquery.com/change/

The change event is sent to an element when its value changes. This event is limited to <input> elements, <textarea> boxes and <select> elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.

So your event will never get raised, since label is rendered as <span>.

兰花执着 2024-11-03 04:39:35

好的,根据您的说法,jQuery 本身不会触发该更改的更改事件。您要么必须自己触发元素上的事件,要么从不同的逻辑运行事件处理程序(可能 ASP.NET 提供了一些东西,我不知道)。

就 jQuery 而言,我非常确定您所描述的情况不会触发更改事件。

另外,您应该用“编辑”标记或其他标记来标记您的编辑,很难找到您对问题所做的更改。

Ok, from what you said jQuery does not fire a change event for that change on itself. You either have to trigger the event on the element yourself, or run the event handler from a different logic (probably ASP.NET provides something, I have no idea).

As far as jQuery is concerned, I am very sure a change event will not be triggered for what you described.

Also, you should've marked your edits with a "Edit" marker or something, it was very hard finding what change you made to the question.

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