如果複選框被選中,如何禁用文本框。 我有幾個文本框和複選框彼此相鄰。如果複選框被選中,jquery禁用下一個元素
HTML:
$("input[name=deposit_checked]").change(function(){
if($("input[name=deposit_checked]").is(":checked")) {
$("input[name=deposit_value]").attr("disabled", false);
} else {
$("input[name=deposit_value]").attr("disabled", true);
}
})
爲了節省時間,我嘗試使用。每()和的.next()函數,但沒有運氣:
<form id="form">
<input type="checkbox" name="deposit_checked"> <input type="text" name="deposit_value">
<input type="checkbox" name="booking_checked"> <input type="text" name="booking_value">
<input type="checkbox" name="referral_checked"> <input type="text" name="referral_value">
</form>
我可以用下面的代碼單獨做到這一點:
$("#form input[type=checkbox]").each(function() {
$(this).change(function(){
if($(this).is(":checked")) {
$(this).next().attr("disabled", false);
} else {
$(this).next().attr("disabled", true);
}
})
})
你的代碼的工作,這是什麼問題? – xdazz