我在网页上有一个GridView。在这个gridview中我有一个Textbox&一个CheckBox。 如果选中CheckBox,则应启用TextBox&如果未选中,则应使用纯javascript禁用TextBox。
请帮我解决这个问题。 提前谢谢。
<asp:GridView ID="grdBasicApproval" runat="server" AutoGenerateColumns="false" Width="90%"
CssClass="mGrid" DataKeyNames="EmpId">
<Columns>
<asp:TemplateField Visible="true" HeaderText="Remark">
<ItemTemplate>
<asp:TextBox ID="txtRemark" runat="server" Width="125px" TextMode="MultiLine" Style="resize: none"></asp:TextBox>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-Width="7%">
<HeaderTemplate>
<asp:CheckBox ID="chkHeader" runat="server" Text="All" onclick="CheckAll(this);"
TextAlign="Left" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkChild" runat="server" onclick="return Check_Click(this);" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
我的JavaScript如下:
$(document).ready(function () {
try {
$("input[type=checkbox][id*=chkChild]").click(function () {
if (this.checked) {
alert("Checked");
$(this).closest("tr", "").find("input[type=TextBox][id*=txtRemark]").attr("disabled", true);
}
else {
alert("UnChecked");
$(this).closest("tr", "").find("input[type=TextBox][id*=txtRemark]").attr("disabled", true);
}
});
} catch (e) {
alert(e);
}
});
答案 0 :(得分:1)
您可以使用RowDataBound中的复选框绑定事件,并将文本框传递给javascript函数。在javascript函数中,您可以启用禁用文本框。
背后的代码
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chkBox = (CheckBox)e.Row.FindControl("checkBoxId");
TextBox textBox = (TextBox )e.Row.FindControl("textBoxId");
chkBox.Attribute.Add("onclick", "EnableDisable(this, '" + textBox.ClientID + "');" );
}
}
的Javascript
function EnableDisable(chkbox, textBoxId)
{
document.getElementById(textBoxId).disabled = !chkbox.checked;
}
答案 1 :(得分:0)
尝试喜欢
HTML
<asp:CheckBox ID="CheckBox" runat="server" />
的Javascript
$(document).ready(function() {
$("input[type=text][id*=TextboxId]").attr("disabled", true);
$("input[type=checkbox][id*=CheckBox]").click(function() {
if (this.checked)
$(this).closest("tr").find("input[type=text][id*=TextboxId]").attr("disabled", false);
else
$(this).closest("tr").find("input[type=text][id*=TextboxId]").attr("disabled", true);
});
});
答案 2 :(得分:0)
您可以使用此代码选中复选框并禁用/启用相对于它的文本框
<asp:TextBox runat="server" ID="txt_Text1"></asp:TextBox>
<asp:CheckBox runat="server" ID="chk_Check" onclick="ChangeText();" />
<script>
function ChangeText() {
var chkb = document.getElementById('<%= chk_Check.ClientID %>');
if(chkb.checked)
document.getElementById('<%= txt_Text1.ClientID %>').disabled = true;
else
document.getElementById('<%= txt_Text1.ClientID %>').disabled = false;
}
</script>