zoukankan      html  css  js  c++  java
  • table获取checkbox是否选中的几种方法

    function test() {

        $(".table tbody tr").find("td:first input:checkbox").each(function () {
            var ischecked = $(this).prop("checked");
            alert(ischecked);

        });

        $(".table input:checkbox").each(function () {
            if ($(this).attr("checked") == true) {
                alert($("table input:checkbox").index(this))
            }
        });

        $(".table tr td:eq(0)").each(function () {

            $(this).find("input[type=checkbox]").prop("checked", "checked");

        });


        var data = [];

        $(".table tbody tr").find(":checkbox:checked").each(function () {
            var val = $(this).parent().next().text();
            data.push(val);
            alert(val);
        });
        alert(data);
    }

    function ck() {

        ////正确 1
        var tbodyObj = document.getElementById('tbodydata');
        $("table :checkbox").each(function (key, value)
        {
            if ($(value).prop('checked'))
            {
                alert(tbodyObj.rows[key].cells[1].innerHTML);
                alert(tbodyObj.rows[key].cells[2].innerHTML);
            }
        })

        //获取选中的数据组  案例           
        var array = $(".table tbody input[type=checkbox]:checked").map(function () {
            return { "cell2": $.trim($(this).closest("tr").find("td:eq(2)").text()), "cell4": $.trim($(this).closest("tr").find("td:eq(4)").text()) };
        }).get();
        $.each(array, function (i, d) { alert(d.cell2 + "|" + d.cell4); })

        //$.each(array, function (i, d) { alert(d.cell2 + "|" + d.cell4); })

        //获取选中的数据组  测试组装值是否正确              
        var array = $(".table tbody input[type=checkbox]:checked").map(function () {
            alert($.trim($(this).closest("tr").find("td:eq(1)").text()));
            alert($.trim($(this).closest("tr").find("td:eq(2)").text()));
            alert($.trim($(this).closest("tr").find("td:eq(3)").text()));
            alert($.trim($(this).closest("tr").find("td:eq(2)").text()));
            //return { "cell2": $.trim($(this).closest("tr").find("td:eq(2)").text()), "cell4": $.trim($(this).closest("tr").find("td:eq(4)").text()) };
        }).get();

        ///把取出来的的值放入数组
        var array = $(".table tbody input[type=checkbox]:checked").map(function () {
            return {
                "code": $.trim($(this).closest("tr").find("td:eq(2)").text()),
                "name": $.trim($(this).closest("tr").find("td:eq(4)").text())
            };
        }).get();
        $.each(array, function (i, d) { alert(d.code + "|" + d.name); })
    }

    function test2() {
        var selectedData = [];
        $(":checkbox:checked").each(function () {
            var tablerow = $(this).parent("tr");
            var code = tablerow.find("[name='p_code']").val();
            var name = tablerow.find("[name='p_name']").val();
            var price = tablerow.find("[name='p_price']").val();
            selectedData.push({ Code: code, Name: name, Price: price });
        });

        var selectedData2 = [];
        $(":checkbox:checked").each(function () {
            var tablerow = $(this).parent("tr");
            var code = tablerow.find("[name='p_code']").val();
            var name = tablerow.find("[name='p_name']").val();
            var price = tablerow.find("[name='p_price']").val();
            selectedData2.push({ Code: code, Name: name, Price: price });
        });
        var selectedData3 = [];
        $(":checkbox:checked", ".table").each(function () {
                var tablerow = $(this).parent("tr");
                var code = tablerow.find("[name='p_code']").val();
                var name = tablerow.find("[name='p_name']").val();
                var price = tablerow.find("[name='p_price']").val();
                selectedData3.push({ Code: code, Name: name, Price: price });
            });

    }

  • 相关阅读:
    IDEA 现有项目连接SVN
    图片预加载,懒加载
    element-ui table中排序 取消表格默认排序问题
    Linux vim编辑命令
    linux mount 硬盘挂载和卸载
    Linux mke2fs 硬盘格式化
    Linux rm 删除文件
    linux 创建和删除目录
    linux 配置IP
    kickstart自动化安装--tftp+nfs+dhcp
  • 原文地址:https://www.cnblogs.com/jpfss/p/7656356.html
Copyright © 2011-2022 走看看