jQuery 与 ASP.NET WebForms - 禁用文本框
另一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想不出一种方法来增加分配给皮肤文件中的控件的CSS类名(phoenix是正确的,类名需要添加到同一属性中)。
不过我可以想到一些解决方法:
-->您可以将所有要禁用的文本框包装在具有给定类的 div 中:
然后通过选择禁用它们:
-->您可以在要禁用的文本框的 ID 中包含字符串:
然后像这样禁用它们:
-->您可以向文本框添加自定义属性:
然后像这样禁用它们:
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:
and then disable them by selecting:
--> You can include character strings in the ID of the textboxes you want disabled:
and then disable them like so:
--> You can add a custom attribute to your textbox:
and then disable them like so:
您的 HTML 标记不正确。
您不能像代码中那样添加两个类。
可以像这样添加两个类
,而不是像
为什么不使用 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
and not like
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.