2010-07-22 183 views
0

我有一個輸入字段的表,我想要1)將第一列從輸入字段更改爲正常的td字段(其工作)和2)更改其他td字段的名稱屬性在行內。 (這與以前的發佈Retrieve an Input fields' name, and then change it有關,但是是一個不同的問題)。
我想這一點:jQuery - 嵌套循環問題

<tr id="row_0" class="dataRow"> 
    <td><input type="text" class="tabcell" name="_0-2" value="AFB" /><td> 
    <td><input type="text" class="tabcell" name="_0-3" value=7.0 /><td> 
    <td><input type="text" class="tabcell" name="_0-7" value=7.6 /></td> 
    .... 
<tr id="row_1" class="dataRow"> 
    <td><input type="text" class="tabcell" name="_1-2" value="TANKERS" /><td> 

替換爲:

<tr id="row_0" class="dataRow"> 
    <td name="resource">AFB</td> 
    <td><input type="text" class="tabcell" name="AFB_0-3" value=7.0 /><td> 
    <td><input type="text" class="tabcell" name="AFB_0-7" Bvalue=7.6/><td> 
    ... 
<tr id="row_1" class="dataRow"> 
    <td name="resource">TANKERS</td> 
    ... 

我jQuery代碼是:

function setRowLabels() { 
    var row = []; 
    var rowCategory = ""; 

    $('.dataRow').each(function(i) { 
     // get the current resource abbreviation value 
     rowCategory = $('td:eq(0) input', this).val(); 

     // replace the contents of the first column (an input field) with a td field 
     $('td:eq(0)', this).replaceWith('<td name="resource">' + rowCategory + '</td>'); 

     var colName = ""; 
     // for each input field .... 
     $('input', this).each(function(j) { 
      // get the current name 
      currName = $('input').attr('name'); 
      $('input').attr('name', rowCategory + currName); 
     }); 
    }); 
} 

但這種情況正在返回:

<tr id="row_0" class="dataRow"> 
    <td name="resource">AFB</td> 
    <td><input type="text" class="tabcell" name="...TANKERSAFBAFBAFBAFBAFBAFBAFBAFB" value=7.0 /><td> 
    <td><input type="text" class="tabcell" name="...TANKERSAFBAFBAFBAFBAFBAFBAFBAFB" Bvalue=7.6/><td> 

所以多次重複第1列的名稱/標籤,再加上每隔一行的名稱/標籤。
再次,任何幫助/指針是受歡迎的。

維克

回答

1

嘗試在你的內循環使用$(本)替換$( '輸入')。

$('input')返回每個輸入,而不僅僅是當前輸入。

function setRowLabels() { 
    var row = []; 
    var rowCategory = ""; 

    $('.dataRow').each(function(i) { 
     // get the current resource abbreviation value 
     rowCategory = $('td:eq(0) input', this).val(); 

     // replace the contents of the first column (an input field) with a td field 
     $('td:eq(0)', this).replaceWith('<td name="resource">' + rowCategory + '</td>'); 

     var colName = ""; 
     // for each input field .... 
     $('input', this).each(function(j) { 
      // get the current name 
      currName = $(this).attr('name'); 
      //   ^^^^^^^^ $('input') calls every input and not only the current one. 
      $(this).attr('name', rowCategory + currName); 
      //^^^^ 
     }); 
    }); 
} 
+0

嗯,我得到相同的結果如前所述: name =「... TANKERSAFBAFBAFBAFBAFBAFBAFBAFB」.... – Vic 2010-07-22 20:06:01

+0

對不起,但你需要在兩個地方改變它!我已經更新了我的回答 – jigfox 2010-07-22 20:26:18

+0

謝謝Jens,那很有效。我開始更好地理解jQuery是如何工作的。 $('input')引用每個輸入,而$(this)。指的是循環內的當前輸入。 Vic – Vic 2010-07-22 20:34:47

1

更改此

$('input', this).each(function(j) { 
     // get the current name 
     currName = $('input').attr('name'); 
     $('input').attr('name', rowCategory + currName); 
    }); 

到這一點,你應該更接近

$('input', this).each(function(j, input) { 
     // get the current name 
     currName = input.attr('name'); 
     input.attr('name', rowCategory + currName); 
    }); 

$。每次通過索引和對象到函數,因此,而不是寫function(j)你可以有function(j, input)和輸入將是你正在循環的元素