简体   繁体   中英

How to get value of checkbox in table?

How can I get value of checkbox in table? I want use it for in this case in order get parameter. Now, please see html table:

<table id="div_func" class="table table-bordered" style="width: 100%">
                                        <thead>
                                            <tr>
                                                <th>
                                                    <input type="checkbox" id="chk_all" /></th>
                                                <th>Name</th>
                                            </tr>
                                        </thead>
                                        <tbody>
        <tr>
        <td> <input type="checkbox" class="checkbox1" id="chk" name="check[]" value="D01" /></td>
        <td>Banana </td>
        </tr>
        <tr>
        <td> <input type="checkbox" class="checkbox1" id="chk" name="check[]" value="D02" /></td>
        <td>Orange </td>
        </tr>
        <tr>
        <td> <input type="checkbox" class="checkbox1" id="chk" name="check[]" value="D03" /></td>
        <td>Apple </td>
        </tr>
                </tbody>
                                    </table>

And this is my script , I use for get value in checkbox , then put parameter

function add_funcgroup() {
            var func_group = [];
            var chk_allow = "";
            var table = document.getElementById("div_func");
            for (var i = 0; i < table.rows.length; i++) {
                if ($('#chk')[i].is(':checked')) {
                    chk_allow = "True";
                }
                else {
                    chk_allow = "False";
                }
                var group_func = {
                    GROUP_MOD_ID: id_temp,
                    FUNCTION_MOD_ID: $('#chk')[i].val(),
                    ALLOW: chk_allow
                };
                func_group[i] = group_func;
            }

            var func_group_temp = {
                FUNC_MOD: func_group
            };

            var DTO = {
                'func_group_temp': func_group_temp
            };
            $.ajax(
            {

And it's not working.

What you have done is right, but you are not outputting it to the table!

var table = $("#div_func");
var value_check = "";
for (var i = 1; i < table.rows.length; i++) {
    if ($('#chk')[i].is(':checked')) {
        value_check += i + ": " + $('#chk')[i].val();
    }
}
alert(value_check);

And you aren't appending it, instead saving!

Try to this

 $('#div_func tbody tr  input:checkbox').each(function() {
    if (this.checked) {
        //Do something
    }
});.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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