Reputation: 971
This is my jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Insert title here</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
</head>
<body>
<table id="one" style="border:1px solid red;">
<caption>Table 1</caption>
<thead>
<tr>
<th></th>
<th >ID</th>
<th> Name</th>
<th>System</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" /></td>
<td>12</td>
<td>Sam</td>
<td>FSS</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>87</td>
<td>Harry</td>
<td>MSS</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>23</td>
<td>Rita</td>
<td>MVV</td>
</tr>
<tr>
<td><input type="checkbox" /></td>
<td>65</td>
<td>Tom</td>
<td>RDD</td>
</tr>
</tbody>
</table>
<br><hr><br>
<button id="add">Add</button>
</body>
</html>
Here, when I click on add button I want to get all the values of the corresponding row that is checked in different variables namely id, name & system that should contain the checked values.
I want these values to be stored in a String (not map). Could you please suggest me a jquery / js to achieve the folllowing
UPDATE
If I have a hidden field along with the checkbox how can I get its value? For example
<td>
<input type="checkbox" />
<input type="hidden" value="secret" id="alertTyp" />
</td>
Upvotes: 2
Views: 17256
Reputation: 28837
Try this:
var stringresult = '';
$('#add').on('click', function () {
$('input:checked').each(function () {
$this = $(this);
var one = $this.parent().siblings('td').eq(0).text();
var two = $this.parent().siblings('td').eq(1).text();
var three = $this.parent().siblings('td').eq(2).text();
alert(one + ' ' + two + ' ' + three);
//or just
stringresult += $this.parent().siblings('td').text();
});
console.log(stringresult);
});
Upvotes: 1
Reputation: 3641
Please see my fiddle for solution
[http://jsfiddle.net/a4WMc/]
Upvotes: 1
Reputation: 5578
If you want the strings in <td>
s, here is jQuery code for that:
var str = "";
$('#add').click(function(){
$('input:checkbox:checked').filter(function(){
str = $(this).closest('tr').text();
});
});
Upvotes: 1
Reputation: 3436
I would do something like this (untested code!!!) :
var myRow = $('input[type="checkbox"]:checked').parents('tr:first');
var result;
myRow.children('td').each(){
result += $(this).html() + "|";
}
this could work if there was only 1 checked checkbox... otherwise you need to cycle through all the checked checkboxes to get values...
Upvotes: 0
Reputation: 180
You can iterate through all the rows...the question how performand would that be:
var str = '';
$('#one').find('tbody').find('tr').each(function()
{
if($(this).children().eq(0).attr('checked') == 'checked')
{
$(this).find('td').each(function()
{
str += $(this).text();
});
}
});
or something like that ...i don't have the time atm to test this sorry.
Upvotes: 0