复选框数组的问题 [JS, Jquery]

Problems with checkboxes arrays [JS, Jquery]

本文关键字:Jquery JS 数组 问题 复选框      更新时间:2023-09-26

在我的 html 代码中,我有 2 个复选框数组,如下所示:

<?php for($i=0;> i<array.length;i++){?>
    <input type="checkbox" name="taski[]">
    <input type="checkbox" name="tooli[]" disabled>
<?php }?>

我想用 JS/Jquery 做什么 如果选中 taski 中的复选框,则 tooli 中具有相同索引的复选框将启用。

请参阅代码中的内联注释:

// Bind change event to all the elements having name as taski[]
$('[name="taski[]"]').on('change', function() {
    // If this checkbox is checked then disable the next checkbox to this
    $(this).next(':checkbox').prop('disabled', !$(this).is(':checked'));

    // OR
    $(this).next(':checkbox').prop('disabled', !this.checked);
});

演示:http://jsfiddle.net/tusharj/j35xk2ep/

演示:http://jsfiddle.net/80L44fjx/(感谢@RGraham)