簡體   English   中英

在jQuery中,如何獲取使用復選框選擇的表的特定行的值?

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

這是我的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>

在這里,當我單擊添加按鈕時,我想獲取在不同變量(即id,name和system中應檢查的值)中檢查的相應行的所有值。

我希望這些值存儲在字符串(不是映射)中。 你能建議我一個jQuery / JS實現以下

UPDATE

如果我的復選框旁邊有一個隱藏字段,如何獲取其值? 例如

<td>
<input type="checkbox" />
<input type="hidden" value="secret" id="alertTyp" />
</td>

嘗試這個:

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

此處演示

如果您想要<td>的字符串,請使用以下jQuery代碼:

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

DEMO

請看我的小提琴尋求解決方案

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

http://jsfiddle.net/a4WMc/

嘗試這個:

$('#alertTyp').val()

您可以遍歷所有行...性能如何的問題,那將是:

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

或類似的東西...我沒有時間atm來測試這個抱歉。

我會做這樣的事情(未經測試的代碼!!!):

var myRow = $('input[type="checkbox"]:checked').parents('tr:first');
var result;
myRow.children('td').each(){
    result += $(this).html() + "|";
}

如果只有1個選中的復選框,這可能會起作用...否則,您需要循環遍歷所有選中的復選框才能獲取值...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM