如何使用JQuery在Gridview中获取Textbox的ID?

时间:2017-01-11 10:44:43

标签: javascript jquery asp.net

我已经搜索了一段时间,但我无法找到我的情况的答案

这是我的问题:

我在 Gridview 中有文本框,如下所示:

        <asp:TemplateField HeaderText="<%$ Resources:DCAAStrategicManagement, obj_lblStandardWeight  %>" ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                <asp:TextBox runat="server" ID="txtStandardWeight" onkeypress="return onlyNumbers();" Text='<%# Eval("StandardWeight") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>

我需要使用 JQuery TextBox 上设置模糊事件

这是我尝试实现这一目标:

 $("input[id*='<%=txtStandardWeight.ClientID %>']").blur(function () {
    Read();
    var currentSum = document.getElementById('<%=hidden.ClientID %>').value;
    var oldLabelData = $('#<%= lblStandardWeightCurrently.ClientID %>').value;
    var newLabelData = oldLabelData + currentSum;
    $('#<%= lblStandardWeightCurrently.ClientID %>').value(newLabelData);
 })

每当lblStandardWeightCurrently出现时,此功能都应更改blur 文字。 但 lblStandardWeightCurrently 标签没有变化..

阅读()功能:

function Read() {
    debugger;
    var oDataGrid = document.getElementById("<%= grdPlanObjectivesStandardWeights.ClientID %>");
    var tableRows = oDataGrid.rows;
    var sum = 0;
    for (var i = 1; i < tableRows.length; i++) {
        var col1 = oDataGrid.rows[i].cells[2];
        for (j = 0; j < col1.childNodes.length; j++) {
            if (col1.childNodes[j].type == "text") {
                if (!isNaN(col1.childNodes[j].value) && col1.childNodes[j].value != "") {
                    sum += parseInt(col1.childNodes[j].value)
                }
            }
        }
    }
    if (!isNaN(sum)) {
        document.getElementById('<%=hidden.ClientID %>').value = sum;
    }
}

我认为问题在于: $("input[id*='<%=txtStandardWeight.ClientID %>']").blur(function () 因为我无法调试此功能。 任何帮助,将不胜感激。

2 个答案:

答案 0 :(得分:3)

请注意,您的文本框位于模板中。这意味着它实际上不存在,直到网格视图是数据绑定,甚至它是什么,有许多文本框,每行一个,每个具有不同的ID。这意味着您无法通过txtStandardWeight简单地访问它。

但是,既然你正在使用jQuery,为什么不给文本框分配一些类,这样你可以轻松地查询它?

<asp:TextBox runat="server" ID="txtStandardWeight" CssClass="weightText" ...

$("input.weightText").blur(function () {

还有一件事 - 确保您的javascript在.aspx文件中定义,否则语法<%# %>无法正常工作。

答案 1 :(得分:1)

$().ready(function () {
            $("#<%= text.ClientID%>").find("tr").each(function () {
                $(this).find("td").each(function () {
                    $(this).find("input[id*=txtStandardWeight]").blur(function () {
                        alert("Hello");
                    });
                });
            });
        });

我们举个例子,其中 gridview id =文字文本框 id = txtStandardWeight 然后我们必须遍历每一行和每列以获得正确的结果,当您找到文本框时,请根据您的需要管理正常运行。