我正在处理Days Available表,我想知道如何让这些复选框并行工作.例如,如果选中日期星期一,则将选中星期一上午、星期一下午、星期一晚上,但是,如果其中一个未选中,则也应取消选中日期星期一.see screenshot

<div class="availability-wrap">
<table>
    <thead>
        <th></th>
        <th>Morning</th>
        <th>Afternoon</th>
        <th>Evening</th>
    </thead>
    <tbody>
        <tr>
            <td>
                <!-- dev notes: don't catch this checkbox data (used only for UI/UX purposes) -->
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="monday" id="monday" value="Monday" class="form-control">
                    <label for="monday">
                        <span>Monday</span>
                    </label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="monday-morning" id="monday-morning" value="Monday morning" class="form-control" checked>
                    <label for="monday-morning"></label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="monday-afternoon" id="monday-afternoon" value="Monday afternoon" class="form-control">
                    <label for="monday-afternoon"></label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="monday-evening" id="monday-evening" value="Monday evening" class="form-control">
                    <label for="monday-evening"></label>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <!-- dev notes: don't catch this checkbox data (used only for UI/UX purposes) -->
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="tuesday" id="tuesday" value="Tuesday" class="form-control">
                    <label for="tuesday">
                        <span>Tuesday</span>
                    </label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="tuesday-morning" id="tuesday-morning" value="Tuesday morning" class="form-control">
                    <label for="tuesday-morning"></label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="tuesday-afternoon" id="tuesday-afternoon" value="Tuesday afternoon" class="form-control">
                    <label for="tuesday-afternoon"></label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="tuesday-evening" id="tuesday-evening" value="Tuesday evening" class="form-control">
                    <label for="tuesday-evening"></label>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <!-- dev notes: don't catch this checkbox data (used only for UI/UX purposes) -->
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="wednesday" id="wednesday" value="Wednesday" class="form-control" checked>
                    <label for="wednesday">
                        <span>Wednesday</span>
                    </label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="wednesday-morning" id="wednesday-morning" value="Wednesday morning" class="form-control" checked>
                    <label for="wednesday-morning"></label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="wednesday-afternoon" id="wednesday-afternoon" value="Wednesday afternoon" class="form-control" checked>
                    <label for="wednesday-afternoon"></label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="wednesday-evening" id="wednesday-evening" value="Wednesday evening" class="form-control" checked>
                    <label for="wednesday-evening"></label>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <!-- dev notes: don't catch this checkbox data (used only for UI/UX purposes) -->
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="thursday" id="thursday" value="Thursday" class="form-control">
                    <label for="thursday">
                        <span>Thursday</span>
                    </label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="thursday-morning" id="thursday-morning" value="Thursday morning" class="form-control">
                    <label for="thursday-morning"></label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="thursday-afternoon" id="thursday-afternoon" value="Thursday afternoon" class="form-control">
                    <label for="thursday-afternoon"></label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="thursday-evening" id="thursday-evening" value="Thursday evening" class="form-control">
                    <label for="thursday-evening"></label>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <!-- dev notes: don't catch this checkbox data (used only for UI/UX purposes) -->
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="friday" id="friday" value="Friday" class="form-control">
                    <label for="friday">
                        <span>Friday</span>
                    </label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="friday-morning" id="friday-morning" value="Friday morning" class="form-control">
                    <label for="friday-morning"></label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="friday-afternoon" id="friday-afternoon" value="Friday afternoon" class="form-control">
                    <label for="friday-afternoon"></label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="friday-evening" id="friday-evening" value="Friday evening" class="form-control">
                    <label for="friday-evening"></label>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <!-- dev notes: don't catch this checkbox data (used only for UI/UX purposes) -->
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="saturday" id="saturday" value="Saturday" class="form-control">
                    <label for="saturday">
                        <span>Saturday</span>
                    </label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="saturday-morning" id="saturday-morning" value="Saturday morning" class="form-control">
                    <label for="saturday-morning"></label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="saturday-afternoon" id="saturday-afternoon" value="Saturday afternoon" class="form-control">
                    <label for="saturday-afternoon"></label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="saturday-evening" id="saturday-evening" value="Saturday evening" class="form-control">
                    <label for="saturday-evening"></label>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <!-- dev notes: don't catch this checkbox data (used only for UI/UX purposes) -->
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="sunday" id="sunday" value="Sunday" class="form-control">
                    <label for="sunday">
                        <span>Sunday</span>
                    </label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="sunday-morning" id="sunday-morning" value="Sunday morning" class="form-control">
                    <label for="sunday-morning"></label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="sunday-afternoon" id="sunday-afternoon" value="Sunday afternoon" class="form-control">
                    <label for="sunday-afternoon"></label>
                </div>
            </td>
            <td>
                <div class="custom-checkbox-wrap">
                    <input type="checkbox" name="sunday-evening" id="sunday-evening" value="Sunday evening" class="form-control">
                    <label for="sunday-evening"></label>
                </div>
            </td>
        </tr>
    </tbody>
</table>
$( '.availability-wrap table input[type="checkbox"]' ).on( 'change', function () {

var checkbox = $( this );
var siblings = $( this ).parents( 'td' ).siblings().find( 'input[type="checkbox"]' );


if ( checkbox.parents( 'td' ).is(':first-child')) {
  siblings.prop( 'checked', checkbox.prop('checked') );

})

这是我试过的:https://codepen.io/pringkr/pen/poLMXJm

推荐答案

当使用表中的位置来定位具有相似行为的特定元素时,它变得非常复杂.此解决方案完全依赖于#id和.className,因此只要遵循以下条件,您就可以将其应用于任何HTML布局:

对于每个主要复选框:

  • 指定日期时分配一个#id
    <input id="monday"...>
    
  • 将.ClassName指定为"day"
    <input id="monday" class="day"...>
    

对于每个辅助复选框:

  • 在指定的日期安排它的头等舱
    <input class="monday"...>
    
  • 将它的第二类指定为"时间"

100

Details are commented in example

/**
 * When any .day changes...
 * ...define >day< as the changed .day #id
 * If the changed .day is checked...
 * ...check all checkboxes with the class of "time"
 * and >day<...
 * ...otherwise, uncheck all checkboxes previously
 * mentioned
 */
$('.day').on('change', function() {
  const day = this.id;
  console.log(day);
  if (this.checked) {
    $('.time.' + day).prop('checked', true);
  } else {
    $('.time.' + day).prop('checked', false);
  }
});

/**
 * When any .time is changed...
 * ...define >day< as the changed .time first .className
 * of it's classList.
 * If all checkboxes with the class of "time" and >day<
 * are checked... ⚝ See below
 * ...check the checkbox with the #id of >day<...
 * ...otherwise uncheck the previously mentioned
 */
$('.time').on('change', function() {
  const day = this.classList[0];
  console.log(day);
  if ($('.time.' + day).toArray().every(chk => chk.checked)) {
    $('#' + day).prop('checked', true);
  } else {
    $('#' + day).prop('checked', false);
  }
});
/** ⚝
 * $('.time.' + day).toArray() converts all elements into an array of
 * elements
 * .every(chk => chk.checked) returns true if all elements in array are 
 * checked
 */
.as-console-row::after { content: ''; width: 0; font-size: 0; }
.as-console-row-code { width: 100%; word-break: break-word; }
.as-console-wrapper { min-height: 100%; max-width: 50%; margin-left: 50%; }
<input id='monday' class='day' type='checkbox'> <label for='monday'> Monday</label><br>
<input id='mM' class='monday time' type='checkbox'> <label for='mM'> Monday Morning</label><br>
<input id='mA' class='monday time' type='checkbox'> <label for='mA'> Monday Afternoon</label><br>
<input id='mE' class='monday time' type='checkbox'> <label for='mE'> Monday Evening</label><br>
<hr>
<input id='tuesday' class='day' type='checkbox'> <label for='tuesday'> Tuesday</label><br>
<input id='tM' class='tuesday time' type='checkbox'> <label for='tM'> Tuesday Morning</label><br>
<input id='tA' class='tuesday time' type='checkbox'> <label for='tA'> Tuesday Afternoon</label><br>
<input id='tE' class='tuesday time' type='checkbox'> <label for='tE'> Tuesday Evening</label><br>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Jquery相关问答推荐

如何在Jquery和Laravel中使用请求传递两个参数给Datatable

为什么我不能在 window.onload 事件的处理程序中将 $ jQuery 对象作为参数传递?

确定元素是否是其父元素的最后一个子元素

jquery克隆div并将其附加在特定div之后

在 Bootstrap 3 中向工具提示添加换行符

未捕获的类型错误:无法读取未定义的属性toLowerCase

基于垂直滚动的jquery添加/删除类?

如何在 jQuery 中修改序列化的表单数据?

未捕获的语法错误:无法在文档上执行querySelector

将返回的 JSON 对象属性转换为(较低的第一个)camelCase

未捕获的 TypeError:data.push 不是函数

是否可以在 beforeunload 弹出窗口中显示自定义消息?

延迟jquery悬停事件?

类方法中的Typescript this

如何在 Bootstrap 中创建切换按钮

在未实现接口 FormData 的对象上调用附加

使用 JQuery 的黄色淡入淡出效果

Twitter Bootstrap 中的树

jQuery - 不可见时获取元素的宽度(显示:无)

清除表jquery