我的 gridview 的一部分如下所示:
<asp:TemplateField HeaderText="Mortality Rate" SortExpression="Other2">
<EditItemTemplate >
<asp:TextBox ID="txtAppLimit" Text='<%#Bind("Other2")%>' runat="server" width="60px" maxlength="14">
</asp:TextBox>
<asp:RangeValidator ID="RVAppLimit" Type="Currency" runat="server" ControlToValidate="txtAppLimit"
Display="Dynamic" ErrorMessage="" Font-Size="8pt"
CssClass="msgerror" MinimumValue="0" MaximumValue="200" OnPreRender="GridView1_PreRender" ValidationGroup="group1">
</asp:RangeValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblAppLimit" Runat="server" Text='<%# Bind("Other2") %>' >
</asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" Width="10%" />
</asp:TemplateField>
当我单击 asp LinkButton 时,我尝试以这种方式获取 asp TextBox ID 和 asp Label ID:
var $arrL = $('#<%=GridView1.ClientID %>').find('span[id$="lblAppLimit"]');
var $lbl = $arrL[0];
($lbl).disabled = true;
alert (($lbl).disabled);
var $arrT = $('#<%=GridView1.ClientID %>').find('input:text[id$="txtAppLimit"]').val();
var $txt = $arrT[0];
alert (($txt).innerHTML);
禁用 lblAppLimit 没有问题,但我无法获取文本框 ID 来禁用它。我上面的代码有什么问题?
补充一点,当我单击浏览器上的“查看源代码”时,我可以看到 lblAppLimit,但看不到 txtAppLimit。
最佳答案
txtAppLimit
位于 EditItemTemplate
中,因此仅在网格处于编辑模式时才有效
所以在编辑模式下尝试这个脚本
var $arrT = $('#<%=GridView1.ClientID %>').find('input:text[id*="txtAppLimit"]').val();
var $txt = $arrT[0];
alert (($txt).innerHTML);
我不知道你为什么使用$arrT[0]
,但这肯定会在编辑模式下工作
$('#<%=GridView1.ClientID %>').find('input:text[id*="txtAppLimit"]').val()
希望这对您有帮助。
关于javascript - 使用 Javascript 禁用 gridview 中的文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37675112/