2014-07-24 36 views
0

我有一个表,这种结构的第2列的值:jQuery的 - 落后活跃单选按钮

<table border="0" cellpadding="0" cellspacing="0" class="databaseTable"> 
    <tr class="header"> 
     <th class="checkable">&nbsp</th> 
     <th>1</th> 
     <th>2</th> 
     <th>3</th> 
    </tr> 
    <tr class="even"> 
     <input name="line" style="float: left;" type="radio" value="2" class="radio" tabindex="1"> 
     </td> 
     <td>121212</td> 
     <td><div style="float:left;">BUBUBU</div></td> 
    </tr> 
</table> 

现在我希望在单选按钮被激活,从表中获取的价值BUBUBU。我可以检查时,单选按钮是否有效:

$('input[name="line"]:radio:checked').val(); 

但现在我想这个特定的行中获取价值BUBUBU。有谁知道我可以使用JQuery获取值吗?

+0

你缺少一个启动TD,现在你的标记无效 – adeneo

回答

1

首先你的HTML是无效的,它应该是:

<table border="0" cellpadding="0" cellspacing="0" class="databaseTable"> 
<tr class="header"> 
<th class="checkable">&nbsp</th> 
<th>1</th> 
<th>2</th> 
<th>3</th> 
</tr> 
<tr class="even"> 
    <td> 
    <input name="line" style="float: left;" type="radio" value="2" class="radio" tabindex="1" /> 
    </td> 
<td>121212</td> 
<td><div style="float:left;">BUBUBU</div></td> 
</tr> 
</table> 

,您可以在单选按钮的单击事件得到它:

$('input[name="line"]').click(function() { 
    alert($(this).val()); // radio button valye 

    alert($(this).closest("tr").find("div").text()); // div value 

}); 

FIDDLE DEMO

+0

感谢您的回答,这正是我一直在寻找的! – mastahb

0

使用此:

var value = $('input[name="line"]').parent().children("td:last-child").text(); 
$('input[name="line"]:radio:checked').val(value); 
0

您可以使用closest()find()

例子:

$(document).ready(function(){ 

    $('input[name="line"]').change(function(){ 
     var isChecked = $(this).is(":checked"); 
     var self = $(this); 
     var value = self.closest(".even").find("td:nth-child(3)").text(); 
     if(isChecked) alert(value); 
    }); 
}); 
0

给该div像<div id="bu">BUBUBU</div>

一个ID,并调用它时,你需要像$("#bu").text()在脚本