使用 Javascript 启用/禁用 <asp:Panel> 及其所有控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5088352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Enable/Disable <asp:Panel> and all its control using Javascript
提问by BreakHead
I have asp:panel
on my page. At run time, I add controls to this panel, and I want to disable/enable all its controls as per business logic.
我有asp:panel
我的网页上。在运行时,我将控件添加到此面板,并且我想根据业务逻辑禁用/启用其所有控件。
I tried with this:
我试过这个:
document.getElementById('mypanel').disabled = true;
But it is not working.
但它不起作用。
Does anyone has any idea to make this work?
有没有人有任何想法使这项工作?
回答by zdyn
An asp:Panel
just produces a div element. This isn't a form control, it's just there for structure.
Anasp:Panel
只是产生一个 div 元素。这不是表单控件,它只是用于结构。
To disable every input control inside of it, if you are using jQuery, try:
要禁用其中的每个输入控件,如果您使用的是 jQuery,请尝试:
$("#<%=mypanel.ClientID%> input").attr("disabled", true);
Or plain ol' JavaScript:
或者普通的 ol' JavaScript:
var controls = document.getElementById("<%=mypanel.ClientID%>").getElementsByTagName("input");
for (var i = 0; i < controls.length; i++)
controls[i].disabled = true;
回答by santosh singh
try the following code snippet
试试下面的代码片段
<div>
<asp:Panel ID="pnl" runat="server">
<asp:TextBox runat="server" />
<asp:TextBox runat="server" />
<asp:CheckBox Text="text" runat="server" />
</asp:Panel>
<input type="button" name="name" value=" Test" onclick="ED();" />
</div>
<script type="text/javascript">
function ED() {
var div_to_disable = document.getElementById('<%=pnl.ClientID %>').getElementsByTagName("input");
var children = div_to_disable;//.childNodes;
for (var i = 0; i < children.length; i++) {
children[i].disabled = true;
};
}
</script>
回答by Suraj
**It is Work 100% **
**这是工作 100% **
function EnableDisableRadio(CheckBox1) {
var controls = document.getElementById("<%=Panel1.ClientID%>").getElementsByTagName("input");
for (var i = 0; i < controls.length; i++)
controls[i].disabled = CheckBox1.checked ? false : true;
}
<asp:CheckBox ID="CheckBox1" runat="server" Text="check" onclick="EnableDisableRadio(this)"/>
<asp:Panel ID="Panel1" runat="server" Width="160px" Enabled="False">
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:RadioButtonList>
</asp:Panel>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
回答by Suraj
<asp:CheckBox ID="CheckBox1" runat="server" Text="check" onclick="EnableDisableRadio(this)"/>
<asp:Panel ID="Panel1" runat="server" Width="160px" Enabled="False">
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:RadioButtonList>
</asp:Panel>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CheckBox ID="CheckBox1" runat="server" Text="check" onclick="EnableDisableRadio(this)"/>
<asp:Panel ID="Panel1" runat="server" Width="160px" Enabled="False">
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:RadioButtonList>
</asp:Panel>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
回答by Null Head
asp:panel is a server control. Why are you manipulating it at client side!? Just use mypanel.enable = false
in code behind
asp:panel 是一个服务器控件。你为什么要在客户端操作它!?只需mypanel.enable = false
在后面的代码中使用