简体   繁体   English

Jquery使用动态值单击函数

[英]Jquery click function using a dynamic value

I have a list of checkboxes which generate tables when checked. 我有一个checkboxes列表, checkboxes会生成表格。 Each table has an All checkbox which when clicked selects all the options of the table. 每个表都有一个All checkbox ,单击该checkbox可选择表的所有选项。 The all checkboxes are all referred to by the same class but have different ID s depending on the table. 所有checkboxes都由同一个类引用,但具有不同的ID具体取决于表。 I am trying to write a code to get the ID of the generated table and use this ID in my select_all function which would allow the ALL checkbox to only affect its respective table's options. 我正在尝试编写一个代码来获取生成的表的ID,并在我的select_all函数中使用此ID,这将允许ALL checkbox仅影响其各自的表的选项。

What I currently have 我现在有什么

ALL Checkbox 所有复选框

<div class="Row">
        <div id="topRow">
            <input type="checkbox" name="tbl" class="tblall" id="all<?php echo $tables_index;?>" value="" />
            <p >ALL</p>
        </div>

ALL Function 所有功能

$(function () {
    $(document).on("click", (".tblall"), function () {
        var className = $("input:checkbox[name='tbl2']").attr('class');
        if (this.checked) {
            // Iterate each checkbox    
            $('.' + className).each(function () {
                this.checked = true;
            });
        } else {
            $('.' + className).each(function () {
                this.checked = false;
            });
        }
    });
});

What I have tried 我试过了什么

I tried to store the ALL checkbox ID in a variable and the use this variable to refer to the checkbox in my function like below: 我试图将ALL复选框ID存储在变量中,并使用此变量来引用我的函数中的复选框,如下所示:

  some function (){
    var allID = $(".tball").attr('id');
    store allID;
}
$(function () {
     var allID = window.sessionStorage.getItem("allID");

    $(document).on("click", ("#"+ allID), function () {

This was not successful as it didn't even select all options of any table. 这是不成功的,因为它甚至没有选择任何表的所有选项。

I also thought if writing a function that fetches the ID and calling the function when the DOM is loaded : 我还想过如果编写一个函数来获取ID并在加载DOM时调用该函数:

function all_Id() {
    var allID;
    if ($("input:checkbox[name='tbl[]']:checked")) {
        allID = $("input:checkbox[name='tbl']").attr('id');
    }
    return allID;
}
$(document).ready(function () {
    all_Id();
});
$(document).ajaxComplete(function () {
    all_Id();
});

What's the best way to achieve what I want? 实现我想要的最好方法是什么?

I guess you need something like this: 我想你需要这样的东西:

$(".tblall").on('change', function () {
    $(this).closest('table').find(':checkbox').prop('checked', this.checked);
});
  1. instead of click apply the change event. 而不是click应用change事件。
  2. when change event happens traverse up to the parent table (as you mentioned) . change事件发生时,遍历到父table (如您所述)
  3. find the checkboxes with :checkbox selector. 找到checkboxes :checkbox选择器。
  4. then apply the property checked if .tball is checked. 如果checked .tball,则应用checked的属性。

this.checked returns boolean as true if checked false if unchecked. 如果未选中则选中falsethis.checked将boolean返回true


A short example is here: 这里有一个简短的例子:

 $(".tblall").on('change', function() { $(this).closest('table').find(':checkbox').prop('checked', this.checked); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr><td><input type='checkbox' class='tblall' />.tblall</td></tr> <tr><td><input type='checkbox' /></td></tr> <tr><td><input type='checkbox' /></td></tr> <tr><td><input type='checkbox' /></td></tr> <tr><td><input type='checkbox' /></td></tr> <tr><td><input type='checkbox' /></td></tr> </table> 

Try this : find all tbl2 checkboxes inside div and make check / uncheck 试试这个:找到div中的所有tbl2复选框并进行检查/取消选中

$(function () {
    $(document).on("change", ".tblall", function () {
        $(this).closest(".Row").find("input:checkbox[name='tbl2']").prop('checked',$(this).is(':checked'));
    });
});

You can try using JQuery's parent-child selector . 您可以尝试使用JQuery的父子选择器

So, if you give each table a unique id, then you can select all checkboxes of a certain class like this: 所以,如果你给每个表一个唯一的id,那么你可以选择某个类的所有复选框,如下所示:

$("#table1 > .checkbox-class").each(function () {
    this.checked = true;
});

Hope that helps. 希望有所帮助。

You could try using jQuery Closest function. 您可以尝试使用jQuery Closest函数。

If you have a table enclosing the checkboxes, the you can use closest to find the "nearest" table tag to your checkbox. 如果您有一个包含复选框的表格,您可以使用最近的表格来查找复选框中最近的表格标签。

Once you have the handle to the table, everything else would seem easy, I suppose. 一旦掌握了桌子,我想其他一切看起来都很简单。

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

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