简体   繁体   English

在jQuery中,如何获取使用复选框选择的表的特定行的值?

[英]In jquery how to get the values of a particular row of a table that is selected using checkbox?

This is my jsp: 这是我的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. 在这里,当我单击添加按钮时,我想获取在不同变量(即id,name和system中应检查的值)中检查的相应行的所有值。

I want these values to be stored in a String (not map). 我希望这些值存储在字符串(不是映射)中。 Could you please suggest me a jquery / js to achieve the folllowing 你能建议我一个jQuery / JS实现以下

UPDATE 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>

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);
});

DEMO HERE 此处演示

If you want the strings in <td> s, here is jQuery code for that: 如果您想要<td>的字符串,请使用以下jQuery代码:

var str = "";
$('#add').click(function(){
    $('input:checkbox:checked').filter(function(){
        str = $(this).closest('tr').text();
    });
});

DEMO DEMO

Please see my fiddle for solution 请看我的小提琴寻求解决方案

[http://jsfiddle.net/a4WMc/]

http://jsfiddle.net/a4WMc/ http://jsfiddle.net/a4WMc/

尝试这个:

$('#alertTyp').val()

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. 或类似的东西...我没有时间atm来测试这个抱歉。

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... 如果只有1个选中的复选框,这可能会起作用...否则,您需要循环遍历所有选中的复选框才能获取值...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM