[英]Jquery get values of all checked rows in table gridview
I have a table like below 我有一张如下表
<table id="mytable">
<tr><th>checked</th><th>id</th><th>text</th></tr>
<tr><td><input id="cb1" type="checkbox" name="checker1"/></td><td>123</td><td>abc</td></tr>
<tr><td><input id="cb1" type="checkbox" name="checker1"/></td><td>456</td><td>def</td></tr>
<tr><td><input id="cb1" type="checkbox" name="checker1"/></td><td>789</td><td>ghi</td></tr>
</table>
I want to retrieve (using jquery) a javascript array of all checked ID's in the table. 我想检索(使用jquery)表中所有已检查ID的javascript数组。
So far I have the following jquery code which on the click of the jqcc button brings me an alert box for each of the checked items, so instead of alert, i need to retrieve the value of the second td and add it to an array, 到目前为止,我有以下jquery代码,点击jqcc按钮为我带来了每个已检查项目的警告框,因此我需要检索第二个td的值并将其添加到数组,而不是警报。
$(document).ready(function() {
var tableControl= document.getElementById('mytable');
$('#jqcc').click(function() {
$('input:checkbox:checked', tableControl).each(function() {
alert('checked');
});
});
});
You should do 你应该做
$(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
will hold the text inside the last td. arrayOfValues
将文本保存在最后一个td中。
EDIT of course you could also use map 编辑当然你也可以使用地图
$(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();
});
});
});
I want to retrieve (using jquery) a javascript array of all checked ID's in the table.
我想检索(使用jquery)表中所有已检查ID的javascript数组。
Try: 尝试:
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(", "));
First of all add the id
as the value parameter of the checkbox
as an input without a value is of little real use: 首先将
id
作为checkbox
的value参数添加为没有值的输入,几乎没有用处:
<table id="mytable">
<tr><th>checked</th><th>id</th></tr>
<tr><td><input id="cb1" type="checkbox" name="checker1" value="123" /></td><td>123</td></tr>
<tr><td><input id="cb1" type="checkbox" name="checker1" value="456" /></td><td>456</td></tr>
<tr><td><input id="cb1" type="checkbox" name="checker1" value="789" /></td><td>789</td></tr>
</table>
Then in jQuery, create your array: 然后在jQuery中,创建你的数组:
var checkedValues = $("input:checkbox:checked", "#mytable").map(function() {
return $(this).val();
}).get();
alert(checkedValues.join(','));
This does what you ask for: 这符合您的要求:
$(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
});
});
http://jsfiddle.net/uEr3n/ http://jsfiddle.net/uEr3n/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.