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() { 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 }); }); }
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: 移动端适配(需补充)
- 下一篇: Angular——作用域($scope)内变量的变量名是动态的