How to get value of checkbox in table?

那年仲夏 提交于 2020-01-25 10:28:06

问题


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.


回答1:


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!




回答2:


Try to this

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


来源:https://stackoverflow.com/questions/30800228/how-to-get-value-of-checkbox-in-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!