javascript 回发后禁用单选按钮失去价值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4998241/
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
Disabled radio button losing value after postback
提问by Alexandre Pepin
I have two radio buttons that are disabled with javascript when the page loads. RadioButton1is checked by default. When I click the button to do a postback, the RadioButton1is no longer checked.
我有两个单选按钮在页面加载时被 javascript 禁用。RadioButton1默认选中。当我单击按钮进行回发时,RadioButton1不再检查 。
Anyone know why ?
有谁知道为什么?
Here's my code sample. The code behind is empty.
这是我的代码示例。后面的代码是空的。
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="group" Checked="true"/>
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="group" />
<asp:Button ID="Button1" runat="server" Text="Button"></asp:Button>
<script type="text/javascript">
window.onload = function () {
var RadioButton1 = document.getElementById('<%= RadioButton1.ClientID %>');
var RadioButton2 = document.getElementById('<%= RadioButton2.ClientID %>');
RadioButton1.disabled = true;
RadioButton2.disabled = true;
};
</script>
采纳答案by Ladislav Mrnka
This is behavior of HTML, not ASP.NET. Once you mark input element as disabled it is no longer posted in request. For text fields this can be avoided by using Readonly insted of Disabled but I think it doesn't work for checkboxes or radio buttons. If you still want to post the value you must send it in hidden field related to the radio button and process it manually on the server.
这是 HTML 的行为,而不是 ASP.NET。一旦您将输入元素标记为禁用,它就不再在请求中发布。对于文本字段,这可以通过使用 Readonly insted of Disabled 来避免,但我认为它不适用于复选框或单选按钮。如果您仍想发布该值,则必须将其发送到与单选按钮相关的隐藏字段中,并在服务器上手动处理。
Edit:
编辑:
Hereyou can read about disabled and readonly elements and about form submission:
在这里您可以阅读有关禁用和只读元素以及表单提交的信息:
Disabled control:
禁用控制:
Disabled control cannot be sucessful.
禁用控制不会成功。
Read-only control:
只读控制:
Read-only elements may be successful.
只读元素可能会成功。
Form submission:
表单提交:
A successful control is valid for submission. Every successful control has its control name paired with its current value as part of submitted form data set. A successful control must be defined within a form element and must have a control name.
However:
- Controls that are disabled cannot be successful.
成功的控制是有效的提交。每个成功的控件都将其控件名称与其当前值配对,作为提交的表单数据集的一部分。成功的控件必须在表单元素中定义并且必须具有控件名称。
然而:
- 禁用的控件无法成功。
回答by CRice
Try making them get the attribute readonly="readonly"instead. A disabled input wont post a value.
尝试让他们获得属性readonly="readonly"。禁用的输入不会发布值。
回答by Frédéric Hamidi
That's because disabled <input>elements don't send any value to the server on postback, and radio buttons don't store their checked state in ViewState, so it can't be persisted that way either.
这是因为禁用的<input>元素不会在回发时向服务器发送任何值,并且单选按钮不会将其选中状态存储在ViewState 中,因此它也不能以这种方式持久化。
Since both RadioButton1and RadioButton2are disabled, their name (group) won't be part of the postback, and ASP.NET will clear them both as a result.
由于RadioButton1和RadioButton2都被禁用,它们的名称 ( group) 不会成为回发的一部分,因此 ASP.NET 将清除它们。
回答by whyleee
If elements disabled than they don't will be sent at postback. Before you do a postback you must enable them first. Example in jQuery:
如果元素被禁用,则不会在回发时发送。在您进行回发之前,您必须先启用它们。jQuery 中的示例:
$('#<%= Button1.ClientID %>').click(function () { RadioButton1.disabled = false;
RadioButton2.disabled = false; });
回答by Rich
Or when generating the disabled checkbox/radiobutton at server level, also generate an <input type='hidden'.. tag for the same name. That will post your value on.
或者在服务器级别生成禁用的复选框/单选按钮时,还<input type='hidden'为同名生成一个.. 标签。那将发布您的价值。

