简体   繁体   English

无法触发模态 window 内的复选框

[英]Can't trigger checkbox inside the modal window

I have webpage where on clicking a button, modal window opens and in that window there is a table containing 3 columns.我有一个网页,点击一个按钮,模态 window 打开,在 window 中有一个包含 3 列的表格。 1 for links, 2nd for share and 3rd for delete. 1个链接,2个分享,3个删除。 2nd and 3rd column only contains checkbox as defined by <td><input style="width:1em;" type="checkbox" id="checkbox"/>&nbsp;</td>第 2 和第 3 列仅包含由<td><input style="width:1em;" type="checkbox" id="checkbox"/>&nbsp;</td>定义的复选框<td><input style="width:1em;" type="checkbox" id="checkbox"/>&nbsp;</td> . <td><input style="width:1em;" type="checkbox" id="checkbox"/>&nbsp;</td>

I wrote a script something like this:我写了一个类似这样的脚本:

            $("#checkbox").click(function(){
                console.log("Checkbox clicked");
            })

I updated the scripts which now works fine but there is still one problem:我更新了现在可以正常工作的脚本,但仍然存在一个问题:

            $(document).on('click', '#checkbox', function () {
                console.log("Checkbox clicked");
                let value = "";
                if ($('#checkbox').is(':checked') == true) {
                    value = $('#checkbox').attr('data-sid');
                    console.log(value);
                }             else if ($('#checkbox').is(':checked') == false) {
                    // value = $('#checkbox').attr('value', 'false1').val();
                    console.log("CAN'T SELECT CHECKBOX");
                }

            })

Below is the modal window:下面是模态 window:

<div class="modal fade" id="ModalGroupLinks" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle"
    aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content showLink-modal">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle">Links associated with Group</h5>
                <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body grid-container">
                <div class="row g-0 justify-content-center">
                    <div class="col-sm-6 link-input-here">
                        <input type="text" class="showLink-modal-gp-name" value="">
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <table class="show-groupLinks">
                            <thead>
                                <tr class="header">
                                    <th>No.</th>
                                    <th>Links</th>
                                    <th>Share</th>
                                    <th>Delete</th>
                                </tr>
                            </thead>
                            <tbody class="showLink-table"></tbody>
                        </table>
                    </div>
                </div>
            </div>
            <div class="modal-ftr">
                <button type="submit" class="button-box" id="footer-btn">Save</button>
            </div>
        </div>
    </div>
</div>

Here <tbody> is empty as I am creating its child elements(which contains input field for checkbox) in success:function() through ajax .这里<tbody>是空的,因为我正在通过ajax success:function() Many rows are generated and each row has 2 checkboxes.生成了许多行,每行有 2 个复选框。 I am now getting the boolean value true or false from only first checkbox that is first row and first checkbox.我现在仅从第一行和第一个复选框的第一个复选框中获取 boolean 值truefalse If I press the other checkboxes, it doesn't gets selected and I get "CANT SELECT THE CHECKBOX" every time from the console.如果我按下其他复选框,它不会被选中,并且每次都从控制台得到“CANT SELECT THE CHECKBOX”。 What is happening here?这里发生了什么?

Use Event Delegation as below:使用事件委托如下:

 $(document).on('click', '#checkbox', function () { console.log("Checkbox clicked"); })
 <:-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https.//maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min:css"> <.-- jQuery library --> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery:min.js"></script> <.-- Latest compiled JavaScript --> <script src="https.//maxcdn.bootstrapcdn.com/bootstrap/3.4;1/js/bootstrap.min:js"></script> <button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#ModalGroupLinks">Open Modal</button> <div class="modal fade" id="ModalGroupLinks" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true"> <div class="modal-dialog modal-dialog-centered" role="document"> <div class="modal-content showLink-modal"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLongTitle">Links associated with Group</h5> <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body grid-container"> <div class="row g-0 justify-content-center"> <div class="col-sm-6 link-input-here"> <input type="text" class="showLink-modal-gp-name" value=""> </div> </div> <div class="row"> <div class="col-md-12"> <table class="show-groupLinks"> <thead> <tr class="header"> <th>No;</th> <th>Links</th> <th>Share</th> <th>Delete</th> </tr> </thead> <tbody class="showLink-table"> <tr> <td><input style="width:1em;" type="checkbox" id="checkbox"/>&nbsp;</td> </tr> </tbody> </table> </div> </div> </div> <div class="modal-ftr"> <button type="submit" class="button-box" id="footer-btn">Save</button> </div> </div> </div> </div>

I found out where the problem lies: In the script below我发现问题出在哪里:在下面的脚本中

            $(document).on('click', '#checkbox', function () {
                console.log("Checkbox clicked");
                let value = "";
                if ($('#checkbox').is(':checked') == true) {
                    value = $('#checkbox').attr('data-sid');
                    console.log(value);
                }else if ($('#checkbox').is(':checked') == false) {
                    // value = $('#checkbox').attr('value', 'false1').val();
                    console.log("CAN'T SELECT CHECKBOX");
                }

            })

In the if and else if condition $('#checkbox') needs to be replaced by the $(this) so it checks whether the clicked checkbox is checked or not.ifelse if条件中$('#checkbox')需要替换为$(this) ,因此它检查是否选中了单击的复选框。 When using $('#checkbox') it gave the attribute value of the first checkbox only even if you click on the later checkboxes.使用$('#checkbox')时,即使您单击后面的复选框,它也只会给出第一个复选框的属性值。

New script should look like this:新脚本应如下所示:

           $(document).on('click', '#checkbox', function () {
                console.log("Checkbox clicked");
                let value = "";
                if ($(this).is(':checked') == true) {
                    value = $(this).attr('data-sid');
                    console.log(value);
                }else if ($(this).is(':checked') == false) {
                    // value = $(this).attr('value', 'false1').val();
                    console.log("CAN'T SELECT CHECKBOX");
                }

            })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM