0
我想僅在複選框被選中時啓用一組複選框。否則,我希望他們保持殘疾。使用jQuery啓用/禁用複選框
這裏是我的代碼:
的jQuery:
$(document).ready(function() {
$(function() {
enable_cb();
$("#university").click(enable_cb);
});
function enable_cb() {
if (this.checked) {
$('input.uni').prop('disabled', false);
} else {
$('input.uni').prop('disabled', true);
}
}
});
HTML:
<form action="test.php" method="post" enctype="multipart/form-data" id="test-form" novalidate>
<input name="table[]" type="checkbox" value="university" id="university"/>
University<br>
<input type="checkbox" disabled="true" class="uni" value="1" />
A<br>
<input type="checkbox" disabled="true" class="uni" value="2" />
B<br>
<input type="checkbox" disabled="true" class="uni" value="3" />
C<br>
<input type="checkbox" disabled="true" class="uni" value="3" />
D<br>
<input name="table[]" type="checkbox" value="buildings" />
Buildings<br>
<input name="table[]" type="checkbox" value="offices" />
Offices<br>
<input name="table[]" type="checkbox" value="halls" />
Halls<br>
<input name="table[]" type="checkbox" value="labs" />
Labs<br>
<input name="table[]" type="checkbox" value="studios" />
Studios<br>
<input name="table[]" type="checkbox" value="machines" />
Machines<br>
<input name="table[]" type="checkbox" value="facilities" />
Facilities<br>
</form>
爲什麼這不是爲我工作嗎?
究竟是什麼錯誤你好嗎? –
它在這裏工作https://jsfiddle.net/yhynuv4L/。你能解釋它是如何不起作用的嗎? – AmmarCSE
您首次調用'enable_cb();'沒有'this'上下文,添加一個[apply](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/ Global_Objects/Function/apply) –