【问题标题】:Checkbox select all checkboxes Laravel复选框选择所有复选框 Laravel
【发布时间】:2020-01-16 08:36:09
【问题描述】:

这是我的问题:

在 PHP Laravel 中,我有一个 foreach 循环,可以在屏幕上显示消息,包括一个复选框。每条消息都有自己的复选框。我在所有消息的顶部还有一个复选框。我想为该复选框分配一个功能以检查所有复选框。我知道过去曾有人问过这个问题,但不幸的是,这些答案对我不起作用。有人可以帮我解决吗?

我正在使用:Laravel、Inspinia、Bootstrap 4

提前致谢!

这是我的代码:


@if(count($messages) > 0)
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>&nbsp;</th>

                    //select all checkboxes
                    <th><input type="checkbox" class=""/></th>

                    <th>@sortablelink('title', trans('messages.title'))</th>
                    <th>@sortablelink('sender_user_id', trans('messages.sender'))</th>
                    <th class="d-none d-sm-table-cell">@sortablelink('created_at', trans('messages.sent_at'))</th>
                </tr>
            </thead>
            <tbody>
            @foreach ($messages as $message)
                <tr id="messagesTable">
                    <td><i class="{{ $message->read ? 'far fa-envelope-open' : 'fas fa-envelope' }}"></i></td>

                    //the checkboxes who need to be selected
                    <td class="project-title">
                        <div class="checkbox p1-1"> 
                            <input type="checkbox" id="message_{{$message->id}}" name="message" value="{{$message->id}}">
                            <label for="message_{{$message->id}}"></label>
                        </div>
                    </td>

                    <td class="project-title">
                        <a href="{{ route('messages.read', [$message->id]) }}">{{$message->title}}</a>
                    </td>
                    <td class="project-title">
                        <a href="{{ route('messages.read', [$message->id]) }}">{{ $message->sender ? $message->sender->name : ''}}</a>
                    </td>
                    <td class="project-title d-none d-sm-table-cell">
                        <a href="{{ route('messages.read', [$message->id]) }}">{{$message->created_at->format('d-m-Y H:i')}}</a>
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
@endif

【问题讨论】:

    标签: php html laravel


    【解决方案1】:

    您可以在jquery 中进行操作

    第一步
    改变这个

    //select all checkboxes
    <th><input type="checkbox" class=""/></th>
    

    到这里

    //select all checkboxes
    <th><input type="checkbox" class="check_all"/></th> //just added a class to this element
    

    第二步
    将 class_name 添加到您的所有 &lt;input type="checkbox"&gt; 我的意思是&lt;input type="checkbox" id="message_{{$message-&gt;id}}" name="message" value="{{$message-&gt;id}}"&gt; 更改为&lt;input type="checkbox" id="message_{{$message-&gt;id}}" name="message" value="{{$message-&gt;id}}" class="custom_name"&gt;

    注意: be sure that all your checkboxes has one class_name instead the one witch if we click it others checked!


    第三步
    将此添加到页脚

    <script
      src="https://code.jquery.com/jquery-3.4.1.js"
      integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
      crossorigin="anonymous"></script>
    <script>
        $(".check_all").on("click", function(){
            $(".custom_name").each(function(){
                $(this).attr("checked", true);
            });
        });
    </script>
    

    我希望这对你有用...

    【讨论】:

    • 我在这一行收到错误$("input[type='checkbox']).each(function(){ 无效或意外令牌
    • 您还需要添加如何将 jQuery 添加到项目中,因为目前还不清楚他是否已经在使用它。
    • @rezash 我想给你一个奖项:年度兄弟。没有抱怨,你回答了这个问题。你这个摇滚老兄。
    • 您的答案工作正常,先生。如何取消选中所有复选框
    • @GanesanJ 检查是否为$(".check_all').is(":checked"),然后添加false 而不是true 以取消选中。
    猜你喜欢
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    相关资源
    最近更新 更多