2014-02-19 220 views
0

我有一个表,在这个表中有两列和五行。在第一列有一个复选框,第二列有一个数据。我想得到检查项目的第二列行值。 我的表格ID是“tb1”,复选框标识“cb1”和第二个字段标识“da1”。如何获得特定的字段值?

enter image description here

我要像“数据2和数据5”的结果也就是说,不管我检查,特定的第二列(<td>)值。

这是可能的,请帮助我。

+1

你是什么意思的“得到”?用什么语言?在客户端或服务器上? – slim

+1

行是动态添加还是手动添加? – Wahtever

+0

提供了一些更多的信息来帮助你,同时,你到目前为止已经尝试了什么,在你的问题中也加上了这个,让每个人都清楚。 –

回答

0

JS Bin

绑定在每一个checkbox和发现第二场”closest

帮助的JavaScript事件

$(function(){ 
    $("#tb1").on('click','input',function(){ 
     var value =($(this).closest('tr').find('[id^=da]').text()) 
    alert(value) 
    }) 
}) 

HTML

<table id="tb1" cellpadding="5" border="1" cellspacing='0' width="200"> 
    <tr> 
    <td id="cb1"><input type='checkbox' /> </td> 
    <td id="da1">Data 1</td> 
    </tr> 
    <tr> 
    <td id="cb2"><input type='checkbox' /> </td> 
    <td id="da2">Data 2</td> 
    </tr> 
    <tr> 
    <td id="cb3"><input type='checkbox' /> </td> 
    <td id="da3">Data 3</td> 
    </tr> 
    <tr> 
    <td id="cb4"><input type='checkbox' /> </td> 
    <td id="da4">Data 4</td> 
    </tr> 


</table> 
0

如果您正在使用CB2和DA2两行,然后在JavaScript中,你可以这样做:只有

如果数据列TD元素:

document.getElementById("da2").innerText 

如果数据列输入元素:

document.getElementById("da2").value;