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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18054487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:36:14  来源:igfitidea点击:

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

javascripthtmljspjquery

提问by user2077648

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

更新

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>

回答by Sergio

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

演示在这里

回答by sohel khalifa

If you want the strings in <td>s, here is jQuery code for that:

如果你想要<td>s 中的字符串,这里是 jQuery 代码:

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

DEMO

演示

回答by Drixson Ose?a

Please see my fiddle for solution

请参阅我的小提琴解决方案

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

http://jsfiddle.net/a4WMc/

http://jsfiddle.net/a4WMc/

回答by user2710367

Try this:

试试这个:

$('#alertTyp').val()

回答by David H?lkeskamp

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.

或类似的东西......我没有时间自动取款机来测试这个抱歉。

回答by Th0rndike

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 个选中的复选框,这可以工作……否则您需要循环查看所有选中的复选框以获取值……