2012-12-27 58 views
1

我得到了一行的索引。列數是靜態的,所以我知道在哪一列放什麼,但我想要的是能夠使用onclick事件處理程序來更改特定單元格中的內容。應用了此onclick事件的元素在表格外部,幷包含要從中複製內容的所有文本輸入。一個簡單的例子使用rowIndex和cellIndex更改表格單元格中的文本

<table id="table1"> 
<tr> 
<td>SomeText</td> 
<td>SomeOtherText</td> 
</tr> 
<tr> 
<td>SomeText2</td> 
<td>SomeOtherText2</td> 
</tr> 
</table> 

<div id="box1"> 
<form> 
<input type="text" name="newText"/> 
<input type="button" onclick="?" />  
</form> 
</div> 

回答

2

您可以使用像得到這個特定的細胞:

$('#table1 tr:eq(1) td:eq(1)') 

這將選擇第二行的第二列(這是基於0票)。您可以使用jQuery.text()jQuery.html()

你可以做一些類似如下:

<table id="table1"> 
<tr> 
<td>SomeText</td> 
<td>SomeOtherText</td> 
</tr> 
<tr> 
<td>SomeText2</td> 
<td>SomeOtherText2</td> 
</tr> 
</table> 

然後在你的onclick方法,你可以這樣做以下:

$('#table1 tr:eq(1) td:eq(1)').text("Whatever text you want"); 

OR

$('#table1 tr:eq(1) td:eq(1)').html("Whatever text you want"); 
+0

唉不錯@Amar這對我有用,你無處不在:) – 2012-12-27 19:58:41

+0

謝謝。希望它也適用於問過這個問題的人。 – Amar

+0

謝謝哈里克里希納 – Reinis

0

可以使用Java腳本和ID。

<table id="table1"> 
<tr> 
<td id=r1c1>SomeText</td> 
<td id=r1c2>SomeOtherText</td> 
</tr> 
<tr> 
<td id=r2c1>SomeText2</td> 
<td id=r2c2>SomeOtherText2</td> 
</tr> 
</table> 
<script> 

function updatetxt(){ 
x=Document.getElementById(r1c1); 
x.value=Document.getElementById(txt1).value 
    }  
</script> 
    <div id="box1"> 
    <form> 
    <input type="text" id=txt1 name="newText" /> 
    <input type="button" onclick="updatetxt()" />  
    </form> 
    </div> 
相關問題