[英]How to get the Textbox's ID inside a Gridview using JQuery?
I have searched for a while but i couldn't find an answer to my situation 我搜索了一段时间,但找不到我的情况的答案
This is my problem: 这是我的问题:
I have a Textbox inside a Gridview like this: 我在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>
And i need to set a blur event on that TextBox using JQuery . 我需要使用JQuery在该TextBox上设置一个模糊事件 。
This is my attempt to achieve that: 这是我尝试实现的目标:
$("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);
})
This function should change the lblStandardWeightCurrently
Text whenever the blur
even occurs. 每当出现blur
时,此函数应更改lblStandardWeightCurrently
Text 。 But There is no changes in lblStandardWeightCurrently label.. 但是lblStandardWeightCurrently标签没有变化。
Read() Function: Read()函数:
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;
}
}
I think the problem is here: $("input[id*='<%=txtStandardWeight.ClientID %>']").blur(function ()
since i can't debug this function . Any help would be appreciated. 我认为问题出在这里: $("input[id*='<%=txtStandardWeight.ClientID %>']").blur(function ()
因为我无法调试此函数。任何帮助将不胜感激。
Note that your textbox is inside a template. 请注意,您的文本框位于模板内。 That means that it actually does not exist until grid view is databound, and even what it is, there are many textboxes, one per row, each having a different ID. 这意味着,直到网格视图绑定了数据,它才真正存在,即使它是什么,也存在许多文本框,每行一个,每个都有不同的ID。 That means that you cannot simply access it by txtStandardWeight
. 这意味着您不能简单地通过txtStandardWeight
访问它。
However since you are using jQuery anyway, why don't assign some class to the textbox which would allow you to query for it easily? 但是,由于无论如何都在使用jQuery,为什么不为该文本框分配一些类,以便您轻松查询呢?
<asp:TextBox runat="server" ID="txtStandardWeight" CssClass="weightText" ...
$("input.weightText").blur(function () {
One more thing - make sure your javascript is defined in .aspx file, otherwise syntax <%# %>
won't work. 还有一件事-确保您的JavaScript在.aspx文件中定义,否则语法<%# %>
将不起作用。
$().ready(function () {
$("#<%= text.ClientID%>").find("tr").each(function () {
$(this).find("td").each(function () {
$(this).find("input[id*=txtStandardWeight]").blur(function () {
alert("Hello");
});
});
});
});
Let's take an example where gridview has id=text and your textbox has an id=txtStandardWeight then we have to traverse each row and column to get proper result and when you find your textbox then manage the proper functioning according to you. 让我们举一个例子,其中gridview的 id为text , 文本框的id为txtStandardWeight,然后我们必须遍历每一行和每一列以获得正确的结果,当您找到文本框时,再根据您的要求管理适当的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.