jQuery 与 ASP.NET WebForms - 禁用文本框

发布于 2024-08-02 16:41:20 字数 2960 浏览 7 评论 0原文

另一个 jQuery 菜鸟问题 - 我做错了什么?

我有一些由 ASP.NET 3.5 Webforms 呈现的 HTML 标记,如下所示:

<input id="ctl01_cphContent_pnlBasicInfo_chkRC" 
       type="checkbox" name="ctl01$cphContent$pnlBasicInfo$chkRC" />
<label for="ctl01_cphContent_cntPromos_pnlBasicInfo_chkRC">Recurrent Charges</label>

<span id="ctl01_cphContent_cntPromos_pnlBasicInfo_lblPromoValidFor" 
      class="rcPromo">Validity:</span>

<span class="rcPromo">
   <input id="ctl01_cphContent_pnlBasicInfo_rbnDiscountValidFor" 
          type="radio" name="ctl01$cphContent$pnlBasicInfo$discountValidFor" 
          value="rbnDiscountValidFor" checked="checked" />
   <label for="ctl01_cphContent_cntPromos_pnlBasicInfo_rbnDiscountValidFor">valid for</label>
</span>
<span class="rcPromo">
   <input id="ctl01_cphContent_pnlBasicInfo_rbnDiscountValidUntil" 
          type="radio" name="ctl01$cphContent$pnlBasicInfo$discountValidFor" 
          value="rbnDiscountValidUntil" />
   <label for="ctl01_cphContent_cntPromos_pnlBasicInfo_rbnDiscountValidUntil">valid until</label>
</span>

<input name="ctl01$cphContent$pnlBasicInfo$txtDiscountMonths" type="text"
       id="ctl01_cphContent_pnlBasicInfo_txtDiscountMonths" 
       class="textbox" class="rcPromo" originalValue="" style="width:30px;" />
<span id="ctl01_cphContent_cntPromos_pnlBasicInfo_lblMonths" class="rcPromo"></span>

<input name="ctl01$cphContent$pnlBasicInfo$txtDiscountUntil" type="text" 
       id="ctl01_cphContent_pnlBasicInfo_txtDiscountUntil" 
       class="textbox" class="rcPromo" originalValue="" style="width:150px;" />
  • 我有一个选中的“chkRC”,我想捕获它并使用它来启用/禁用其他 UI 控件
  • 我有许多标签,输入(类型=无线电)和输入(类型=文本)UI 控件。这些都标有“rcPromo”虚拟 CSS 类
  • 我有一个名为“textbox”的 CSS 类用于正常文本框,“textboxDisabled”用于文本框的禁用状态,在外部引用的 CSS 文件中,工作正常(使用时)在服务器端代码中,即)

我试图在 jQuery 中完成的是:当禁用“chkRC”复选框时,我想禁用所有相关的 UI 控件。

我的 jQuery 看起来像这样:

    $(document).ready(function() {
        $("#<%= chkRC.ClientID %>").click(function() {
            $('.rcPromo > :label').toggleClass('dimmed');

            if (this.checked) {
                $('.rcPromo').removeAttr('disabled');
                $('.rcPromo .textboxDisabled').addClass('textbox').removeClass('textboxDisabled');
            }
            else {
                $('.rcPromo > :input').removeAttr('checked');
                $('.rcPromo .textbox').addClass('textboxDisabled').removeClass('textbox');
                $('.rcPromo').attr('disabled', true);
            }
        });
    });

它对于标签和单选按钮工作得很好 - 但我只是无法让它与文本框一起工作 - 它们只是保持不变,没有任何变化(它们不会被禁用,而且它们不会也不要改变他们的外观来表明他们是残疾人)。

我不明白这一点 - 我确实看到了几个(比示例中多一些)文本框,它们是 HTML 中的 ,并且它们确实有 < code>class="rcPromo" 和 class="textbox" - 那么为什么 jQuery 不查找并更新它们呢?

有什么想法吗?

马克

Another jQuery noob question - what am I doing wrong??

I have some HTML markup rendered by ASP.NET 3.5 webforms which looks like this:

<input id="ctl01_cphContent_pnlBasicInfo_chkRC" 
       type="checkbox" name="ctl01$cphContent$pnlBasicInfo$chkRC" />
<label for="ctl01_cphContent_cntPromos_pnlBasicInfo_chkRC">Recurrent Charges</label>

<span id="ctl01_cphContent_cntPromos_pnlBasicInfo_lblPromoValidFor" 
      class="rcPromo">Validity:</span>

<span class="rcPromo">
   <input id="ctl01_cphContent_pnlBasicInfo_rbnDiscountValidFor" 
          type="radio" name="ctl01$cphContent$pnlBasicInfo$discountValidFor" 
          value="rbnDiscountValidFor" checked="checked" />
   <label for="ctl01_cphContent_cntPromos_pnlBasicInfo_rbnDiscountValidFor">valid for</label>
</span>
<span class="rcPromo">
   <input id="ctl01_cphContent_pnlBasicInfo_rbnDiscountValidUntil" 
          type="radio" name="ctl01$cphContent$pnlBasicInfo$discountValidFor" 
          value="rbnDiscountValidUntil" />
   <label for="ctl01_cphContent_cntPromos_pnlBasicInfo_rbnDiscountValidUntil">valid until</label>
</span>

<input name="ctl01$cphContent$pnlBasicInfo$txtDiscountMonths" type="text"
       id="ctl01_cphContent_pnlBasicInfo_txtDiscountMonths" 
       class="textbox" class="rcPromo" originalValue="" style="width:30px;" />
<span id="ctl01_cphContent_cntPromos_pnlBasicInfo_lblMonths" class="rcPromo"></span>

<input name="ctl01$cphContent$pnlBasicInfo$txtDiscountUntil" type="text" 
       id="ctl01_cphContent_pnlBasicInfo_txtDiscountUntil" 
       class="textbox" class="rcPromo" originalValue="" style="width:150px;" />
  • I have a checked "chkRC" which I want to trap and use to enable/disable other UI controls
  • I have a number of labels, input (type=radio) and input (type=text) UI controls. These are all marked with the "rcPromo" dummy CSS class
  • I have a CSS class called "textbox" for the normal textbox and "textboxDisabled" for the disabled state of the textbox, in an externally referenced CSS file, that work OK (when used in server-side code, that is)

What I'm trying to accomplish in jQuery is this: when the "chkRC" checkbox is disabled, I want to disable all relevant UI controls.

My jQuery looks like this:

    $(document).ready(function() {
        $("#<%= chkRC.ClientID %>").click(function() {
            $('.rcPromo > :label').toggleClass('dimmed');

            if (this.checked) {
                $('.rcPromo').removeAttr('disabled');
                $('.rcPromo .textboxDisabled').addClass('textbox').removeClass('textboxDisabled');
            }
            else {
                $('.rcPromo > :input').removeAttr('checked');
                $('.rcPromo .textbox').addClass('textboxDisabled').removeClass('textbox');
                $('.rcPromo').attr('disabled', true);
            }
        });
    });

It works fine for the labels and the radiobuttons - but I just can't get it to work with the textboxes - they just stay the same all around, nothing changes (they don't get disabled and they don't change their appearance to indicate that they're disabled, either).

I don't understand this - I do see several (a few more than in the sample) textboxes, which are <input type="text"> in HTML, and they do have the class="rcPromo" and class="textbox" on them - so why doesn't jQuery find and update those?

Any ideas?

Marc

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

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

发布评论

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

评论(2

情绪操控生活 2024-08-09 16:41:20

我想不出一种方法来增加分配给皮肤文件中的控件的CSS类名(phoenix是正确的,类名需要添加到同一属性中)。

不过我可以想到一些解决方法:

-->您可以将所有要禁用的文本框包装在具有给定类的 div 中:

<div class="disable_textbox"><asp:textbox id="".../></div>

然后通过选择禁用它们:

$('.disable_textbox input').attr('disabled', true);

-->您可以在要禁用的文本框的 ID 中包含字符串:

<asp:textbox id="txtDiscountUntil_DisableMe" ... />

然后像这样禁用它们:

$("input[id*='DisableMe']").attr('disabled', true);

-->您可以向文本框添加自定义属性:

txtDiscountUntil.Attributes.Add("disableme", "true");

然后像这样禁用它们:

$("input[disableme='true']").attr('disabled', true);

I can't think of a way to augment the css class names that are assigned to controls from the skin file (phoenix is correct, the class names need to be added in the same attribute).

I can think of a few workarounds though:

--> You can wrap all the textboxes you want disabled in a div with a given class:

<div class="disable_textbox"><asp:textbox id="".../></div>

and then disable them by selecting:

$('.disable_textbox input').attr('disabled', true);

--> You can include character strings in the ID of the textboxes you want disabled:

<asp:textbox id="txtDiscountUntil_DisableMe" ... />

and then disable them like so:

$("input[id*='DisableMe']").attr('disabled', true);

--> You can add a custom attribute to your textbox:

txtDiscountUntil.Attributes.Add("disableme", "true");

and then disable them like so:

$("input[disableme='true']").attr('disabled', true);
深海夜未眠 2024-08-09 16:41:20

您的 HTML 标记不正确。

您不能像代码中那样添加两个类。

可以像这样添加两个类

<input type="text" class="Class1 Class2" />

,而不是像

<input type="text" class="Class1" class="Class2" />

为什么不使用 hasClass 来检查是否元素是否设置了此类?

我认为你必须在两个类的 OR 条件中给出这个。

Your HTML markup is not the correct one.

You can't add two classes like the one in your code.

Two classes can be added like this

<input type="text" class="Class1 Class2" />

and not like

<input type="text" class="Class1" class="Class2" />

Why don't you use hasClass to check whether the element has this class set or not?

I think you have to give this in an OR condition for the two classes.

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