Jquery获取表gridview中所有已检查行的值
我有一张如下表
checked id text 123 abc 456 def 789 ghi
我想检索(使用jquery)表中所有已检查ID的javascript数组。
到目前为止,我有以下jquery代码,点击jqcc按钮为我带来了每个已检查项目的警告框,因此我需要检索第二个td的值并将其添加到数组,而不是警报。
$(document).ready(function() { var tableControl= document.getElementById('mytable'); $('#jqcc').click(function() { $('input:checkbox:checked', tableControl).each(function() { alert('checked'); }); }); });
你应该做
$(document).ready(function() { var tableControl= document.getElementById('mytable'); var arrayOfValues = []; $('#jqcc').click(function() { $('input:checkbox:checked', tableControl).each(function() { arrayOfValues.push($(this).closest('tr').find('td:last').text()); }).get(); }); });
arrayOfValues
将文本保存在最后一个td中。
编辑当然你也可以使用地图
$(document).ready(function() { var tableControl= document.getElementById('mytable'); var arrayOfValues = []; $('#jqcc').click(function() { arrayOfValues = $('input:checkbox:checked', tableControl).map(function() { return $(this).closest('tr').find('td:last').text(); }); }); });
我想检索(使用jquery)表中所有已检查ID的javascript数组。
尝试:
var ids = $("#mytable tr:has(input:checked)").map(function() { var $tr = $(this); var id = $tr.find("td:last").text(); return id; }).toArray(); alert(ids.join(", "));
首先将id
作为checkbox
的value参数添加为没有值的输入,几乎没有用处:
checked id 123 456 789
然后在jQuery中,创建你的数组:
var checkedValues = $("input:checkbox:checked", "#mytable").map(function() { return $(this).val(); }).get(); alert(checkedValues.join(','));
工作小提琴
var tableControl = document.getElementById('mytable'); $('#jqcc').click(function() { var result = [] $('input:checkbox:checked', tableControl).each(function() { result.push($(this).parent().next().text()); }); alert(result); });
见演示
这符合您的要求:
$(document).ready(function() { var tableControl= document.getElementById('mytable'); $('#jqcc').click(function() { var obj = new Array(); $('input:checkbox:checked', tableControl).each(function() { var innertext = $(this).parent().next().text(); obj.push(innertext); }); console.debug(obj); // Write it to the console }); });