0

我有一个按如下方式呈现的gridview

<table cellspacing="0" cellpadding="4" rules="cols" id="ContentPlaceHolder1_ucstockcount1_GridView1" style="color:Black;background-color:White;border-color:#DEDFDE;border-width:1px;border-style:None;width:100%;border-collapse:collapse;">
    <tr style="color:White;background-color:#6B696B;font-weight:bold;">
        <th scope="col">Sr.no</th><th scope="col">Style</th><th scope="col">Stk Last Mon</th><th scope="col">Stk Received</th><th scope="col">Stk This Mon</th><th scope="col">Change</th><th scope="col">Price</th><th scope="col">Value</th>
    </tr><tr style="background-color:#F7F7DE;">
        <td>1</td><td>T1-34H</td><td>0</td><td>0</td><td>
                            <input name="ctl00$ContentPlaceHolder1$ucstockcount1$GridView1$ctl02$TextBox2" type="text" id="ContentPlaceHolder1_ucstockcount1_GridView1_TextBox2_0" onblur="updatevals(&quot;ContentPlaceHolder1_ucstockcount1_GridView1_TextBox2_0&quot;,0,0,5.00,&quot;ContentPlaceHolder1_ucstockcount1_GridView1_Label1_0&quot;,&quot;ContentPlaceHolder1_ucstockcount1_GridView1_Label2_0&quot;)" />

                        </td><td>
                            <span id="ContentPlaceHolder1_ucstockcount1_GridView1_Label1_0">0</span>
                        </td><td>5.00</td><td>
                            <span id="ContentPlaceHolder1_ucstockcount1_GridView1_Label2_0">0</span>
                        </td>
    </tr><tr style="background-color:White;">
        <td>2</td><td>T1-43B</td><td>0</td><td>0</td><td>
                            <input name="ctl00$ContentPlaceHolder1$ucstockcount1$GridView1$ctl03$TextBox2" type="text" id="ContentPlaceHolder1_ucstockcount1_GridView1_TextBox2_1" onblur="updatevals(&quot;ContentPlaceHolder1_ucstockcount1_GridView1_TextBox2_1&quot;,0,0,5.00,&quot;ContentPlaceHolder1_ucstockcount1_GridView1_Label1_1&quot;,&quot;ContentPlaceHolder1_ucstockcount1_GridView1_Label2_1&quot;)" />

                        </td><td>
                            <span id="ContentPlaceHolder1_ucstockcount1_GridView1_Label1_1">0</span>
                        </td><td>5.00</td><td>
                            <span id="ContentPlaceHolder1_ucstockcount1_GridView1_Label2_1">0</span>
                        </td>
    </tr>

我使用以下代码在客户端提取值。

function updatea(grid) {
$('#' + grid + ' tr').each(function (i) {
    var style = "";
    var lastmon = 0;
    var received = 0;
    var thismon = 0;
    var change = 0;
    var price = 0;
    var value = 0;
    $(this).children('td').each(function (j) {
        if (j == 2) {
            style = $(this).html();
        } else if (j == 3) {
            lastmon = $(this).html();
        } else if (j == 4) {
            received = $(this).html();
        } else if (j == 5) {
            thismon = $(this).children('input').val().trim();
            alert(thismon);
        }
        //            else if (j == 6) {
        //                change = $(this).children('span').val().trim();
        //                alert(change);
        //            }
        //            alert($(this).html());
    });
});

}

直到第 4 列,我才能根据需要获取值,但是当我需要提取第 5 列内的文本框的值时,我收到错误 TypeError: $(...).children(...).val(. ..) 未定义

使用 jQuery 从 Traverse html 表中获取了代码

请帮忙。谢谢

4

4 回答 4

4

两个问题,索引从0索引4的列开始有输入元素,第二个第一行没有输入元素

function updatea(grid) {
    //start processing from row 1
    $('#' + grid + ' tr').slice(1).each(function (i) {
        var style = "";
        var lastmon = 0;
        var received = 0;
        var thismon = 0;
        var change = 0;
        var price = 0;
        var value = 0;
        $(this).children('td').each(function (j) {
            if (j == 1) {
                style = $(this).html();
            } else if (j == 2) {
                lastmon = $(this).html();
            } else if (j == 3) {
                received = $(this).html();
            //column 5 has index 4
            } else if (j == 4) {
                thismon = $(this).children('input').val().trim();
                alert(thismon);
            }
        });
    });
}

演示:小提琴

于 2013-09-20T11:31:32.520 回答
1

in 的索引each()是从零开始的,第五个 TD 没有输入,第四个有:

} else if (j == 4) {
    thismon = $(this).children('input').val().trim();
    alert(thismon);
}
于 2013-09-20T11:30:22.857 回答
1

您可以通过这种方式更轻松地进行选择

$('#' + grid + ' tr').slice(1).each(function (i) {
    var style = $('td:eq(1)', this).html();
    var lastmon = $('td:eq(2)', this).html();
    var received = $('td:eq(3)', this).html();
    var thismon = $('td:eq(4) input', this).val();
}

+1 给@arun-p-johny.slice(1)

于 2013-09-20T11:59:04.347 回答
0

有时 val() 函数在某些情况下对我不起作用。您可以测试attr('value')而不是,val()或者您可以将 children() 更改为$($(this).find('input')).val().trim()or$(this+'input').val().trim()$(this).children().val().trim()

所以我现在不记得了,但有时返回的对象不是 DOM 对象。

最后就可以测试了$($(this).children('input')).val().trim()

正如你所看到的,有太多的动作找不到任何东西。我只是关注主要问题,所以你需要测试这些,如果其中任何一个不起作用,请告诉我。

但我认为主要问题是返回值不是 DOM 对象。所以它试图到达非对象并给出错误。

于 2013-09-20T11:37:46.103 回答