У меня есть textbox
и checkbox
, когда checkbox
снят, мне нужно отключил textbox
и когда пользователь выбирает checkbox
, я хочу, чтобы повторно включить textbox
.JQuery, когда флажок не установлен скрыть текстовое поле
Я попытался это:
код 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
, но он не работает, чтобы снова включить его, что я сделал неправильно?
Update 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', и у меня все еще есть та же проблема, что и на вопрос –
Кажется, для меня работают: http://jsfiddle.net/bryandowning/wcktqdnt/ –