有没有办法将以下功能组合为一个?
$('#box_1 input:checkbox').click(function(){
var $inputs = $('#box_1 input:checkbox')
if($(this).is(':checked')){
$inputs.not(this).prop('disabled',true);
}else{
$inputs.prop('disabled',false);
}
});
$('#box_2 input:checkbox').click(function(){
var $inputs = $('#box_2 input:checkbox')
if($(this).is(':checked')){
$inputs.not(this).prop('disabled',true);
}else{
$inputs.prop('disabled',false);
}
});
还有更多的 box_id。如果有一个比编写 2 公里代码更简单的解决方案就好了
提前非常感谢您的帮助
最佳答案
首先对所有外框进行循环
$('[id^=box_]').each(function(){
var $inputs=$(this).find(':checkbox').change(function(){
$inputs.not(this).prop('disabled', this.checked);
});
});
ID 作为属性选择器有点难看,更喜欢使用公共(public)类
关于javascript - 将相似的 jQuery 函数分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24744213/