【发布时间】:2015-05-06 07:33:11
【问题描述】:
我有一个textbox 和一个checkbox,当checkbox 未选中时,我需要禁用textbox,当用户选择checkbox 时,我想重新启用@987654329 @。
我试过这个:
ASP.NET 代码:
<div class="checkConfiguration">
<asp:CheckBox runat="server" ID="stopGeneralNumber" Text="General Number" CssClass="cbStopReason" Checked="false" />
<asp:TextBox runat="server" Enabled="false"></asp:TextBox>
</div>
jQuery 代码
$('.cbStopReason').on('click', function () {
if ($(this).attr('checked')) {
alert("now checked");
$(this).nextAll("input").removeAttr("disabled");
} else {
alert("now un checked");
$(this).nextAll("input").attr("disabled", "disabled");
}
})
jQuery 代码已经在document ready function 中,但问题是我的代码可以很好地禁用textbox,但重新启用它却不起作用,请问我做错了什么?
更新 1:这是正在生成的实际 HTML
<div class="configurationData">
<div class="checkConfiguration">
<span class="cbStopReason"><input id="stopUrgentNumber" type="checkbox" name="stopUrgentNumber"><label for="stopUrgentNumber">Urgent Number</label></span>
<input name="ctl17" type="text" disabled="disabled" class="aspNetDisabled">
</div>
<div class="checkConfiguration">
<span class="cbStopReason"><input id="stopNurseNurse" type="checkbox" name="stopNurseNurse"><label for="stopNurseNurse">Nurse Number</label></span>
<input name="ctl18" type="text" disabled="disabled" class="aspNetDisabled">
</div>
<div class="checkConfiguration">
<span class="cbStopReason"><input id="stopScheduledNumber" type="checkbox" name="stopScheduledNumber"><label for="stopScheduledNumber">Scheduled Number</label></span>
<input name="ctl19" type="text" disabled="disabled" class="aspNetDisabled">
</div>
<div class="checkConfiguration">
<span class="cbStopReason"><input id="stopClockNumber" type="checkbox" name="stopClockNumber"><label for="stopClockNumber">Clock Number</label></span>
<input name="ctl20" type="text" disabled="disabled" class="aspNetDisabled">
</div>
<div class="checkConfiguration">
<span class="cbStopReason"><input id="stopLastCheckNumber" type="checkbox" name="stopLastCheckNumber"><label for="stopLastCheckNumber">Last Check Number</label></span>
<input name="ctl21" type="text" disabled="disabled" class="aspNetDisabled">
</div>
<div class="checkConfiguration">
<span class="cbStopReason"><input id="stopArrivalNumber" type="checkbox" name="stopArrivalNumber"><label for="stopArrivalNumber">Arrival Number</label></span>
<input name="ctl22" type="text" disabled="disabled" class="aspNetDisabled">
</div>
<div class="checkConfiguration">
<span class="cbStopReason"><input id="stopGeneralNumber" type="checkbox" name="stopGeneralNumber"><label for="stopGeneralNumber">General Number</label></span>
<input name="ctl23" type="text" disabled="disabled" class="aspNetDisabled">
</div>
</div>
【问题讨论】:
-
你试过用
$(this).prop('checked')代替吗? -
@BryanDowning 我刚刚尝试了您的建议,我将您的代码放在了
if语句中,但我仍然遇到问题中描述的相同问题 -
@BryanDowning 但您使用了与我不同的 html,我更新了问题并添加了生成的 html
标签: javascript jquery asp.net checkbox