jQuery - 嵌套循环问题

时间:2010-07-22 18:54:36

标签: jquery

我有一个输入字段表,我希望1)将第一列从输入字段更改为普通的td字段(可以工作)和2)更改行中其他td字段的name属性。 (这与以前的帖子Retrieve an Input fields' name, and then change it有关,但是是另一个问题) 我想要这个:

<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列中的名称/标签,以及每隔一行的名称/标签 再次,欢迎任何帮助/指针。

维克

2 个答案:

答案 0 :(得分:1)

尝试在内循环中用$(this)替换$('input')。

$('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);
            //^^^^
        });
    });
}  

答案 1 :(得分: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),输入将是您循环的元素

相关问题