我有这个 HTML 代码
<input type="checkbox" name="city_pref[]" id="city_all" value="0" checked /><label for="city_all">All</label>
<input type="checkbox" name="city_pref[]" id="city_pref_1" value="Chicago" /><label for="city_pref_1">Chicago</label>
<input type="checkbox" name="city_pref[]" id="city_pref_2" value="Texas" /><label for="city_pref_2">Texas</label>
而且,我有 placed my code in a fiddle ,它工作正常。我真正想做的是,当没有选中任何复选框时,我希望自动选中 all 复选框。
最佳答案
尝试以下操作:
$('input[type=checkbox]').change(function(){
if ($('input[type=checkbox]:checked').length == 0) {
$('#city_all').prop('checked', true)
}
})
关于javascript - 如果没有选中复选框,请选中第一个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11608062/