2013-12-15 39 views
0

我需要循环一个表,并获取行单元格数据变量。我没有问题得到它的工作(在这里工作的小提琴)http://jsfiddle.net/alsosun/8nvPX/3/循环表与复选框,并获取行值jquery移动1.3

但是,当我切换到jquery 1.3移动,我没有得到值传递给变量。

一直在寻找,但找不到差异。代码:

<table id="one"> 
    <tr> 
    <th></th> 
    <th>ID</th> 
    <th>Name</th> 
    <th>System</th> 
    </tr> 

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

    </tbody> 
</table> 
<br> 
<hr> 
<br> 
<button id="add">Add</button> 

脚本tr的孩子(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(); 
    }); 
    alert('This is the whole string: '+stringresult); 
}); 
+0

http://jsfiddle.net/Palestinian/8nvPX/4/ – Omar

+0

只是侥幸 - 明白了'$(“输入[名称='的情况下[] '):checked“)。each(function(){//closest("td").siblings("td”), var code = $(this).closest(“td”)。siblings(“td “).eq(0).text(); var out = $(this).closest(”td“)。siblings(”td“)。eq(1).text(); // var two = $(this).eq(1).text(); alert(code +“”+ out) });' – Cory

+0

@Omar刚刚看到你的。是的,该作品谢谢 - 添加它,以便我可以接受它 – Cory

回答

1

循环不包含input,使用:not():has()选择。

$('#add').on('click', function() { 
    $('input:checked').each(function() { 
     var checked = $(this).closest("tr").find("td:not(:has(input))"); 
     $.each(checked, function() { 
      console.log($(this).text()); 
     }); 
    }); 
}); 

Demo

相关问题