2010-05-27 37 views
1

我有一個表格,其中所有的單元格都是INPUT標籤。我有一個函數,它查找第一個輸入單元格,並用它的值替換它。所以這樣的:jQuery參考HTML表格中的第一列

<tr id="row_0" class="datarow"> 
    <td><input class="tabcell" value="Injuries"></td> 
    <td><input class="tabcell" value="01"></td> 

變成這樣:

<tr id="row_0" class="datarow"> 
    <td>Injuries</td> 
    <td><input class="tabcell" value="01"></td> 

下面是函數的第一部分:

function setRowLabels() { 

    var row = []; 
    $('.dataRow').each(function(i) { 
     row.push($('td input:eq(0)', this).val() + ' -> '); 
     $('td input:eq(0)', this).replaceWith($('td input:eq(0)', this).val()); 
     $('td input:gt(0)', this).each(function(e) { 
    etcetera 

但是,當重新加載頁面時,第一列是不是輸入型,所以它會將第二列更改爲文本!

我可以告訴它只更改第一列,不管類型是什麼?我試過
$('td:eq(0)', this).replaceWith($('td:eq(0)', this).val());
但它不起作用。

任何建議表示讚賞!

回答

1

隨着你的版本,你選擇td而不是input

// Selects the first td in the row 
$('td:eq(0)', this).replaceWith($('td:eq(0)', this).val()); 

試試這個:

// Selects the input of the first td in the row 
$('td:eq(0) input', this).replaceWith($('td:eq(0) input', this).val()); 
+0

當我第一次看到你的迴應時,我以爲你已經輸入了我第一次嘗試的東西。重讀後,我看到你改變了 'td input:eq(0)' to'td:eq(0)input'
它工作! 謝謝! – Vic 2010-05-27 19:33:59

+0

不客氣。很高興工作! – user113716 2010-05-27 19:39:36

0
+0

另外,不知道你是否抓住了這一點,但你有「數據行」作爲您的TR的類屬性值,而「數據行」作爲你的jQuery函數的值。 – 2010-05-27 18:40:14

+0

糟糕,錯字。它們都是dataRow。 – Vic 2010-05-27 19:29:29

+0

CSS td:nth-​​child僞類現在已加入書籤!漂亮的顏色,雖然... – Vic 2010-05-27 19:31:24