我有我有单选按钮以及复选框的asp.net网页。 当单选按钮状态发生任何变化时,我需要更改复选框的文本。更改单选按钮的状态更改复选框的文本
请建议我如何能做到这一点...
我试着用单选按钮的状态的情况下,对我已设置检查框的文本属性更改事件&。 这个我已经在背景.cs文件 中完成,但没有发生。
请建议。 谢谢
我有我有单选按钮以及复选框的asp.net网页。 当单选按钮状态发生任何变化时,我需要更改复选框的文本。更改单选按钮的状态更改复选框的文本
请建议我如何能做到这一点...
我试着用单选按钮的状态的情况下,对我已设置检查框的文本属性更改事件&。 这个我已经在背景.cs文件 中完成,但没有发生。
请建议。 谢谢
您需要设置RadioButton的属性AutoPostBack=true
和处理程序CheckedChanged
事件。
例子:
<asp:RadioButton
ID="RadioButton1"
runat="server"
AutoPostBack="True"
GroupName="Group1"
oncheckedchanged="RadioButton1_CheckedChanged" />
<asp:RadioButton
ID="RadioButton2"
runat="server"
AutoPostBack="True"
GroupName="Group1"
oncheckedchanged="RadioButton1_CheckedChanged" />
<asp:CheckBox ID="CheckBox1" runat="server" />
而后面的代码:
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
CheckBox1.Text = RadioButton1.Checked ? "Checked" : "Not Checked";
}
从你提供我会建议使用Javascript
做到这一点,因为这将极大地改善了用户体验的信息。如果你能提供更多的细节,我可以给你更多的信息。
编辑按你的意见
的标记:
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="RadioButton1">RadioButton1</asp:ListItem>
<asp:ListItem Value="RadioButton2">RadioButton2</asp:ListItem>
</asp:RadioButtonList>
<asp:CheckBox ID="Checkbox1" runat="server" Text="RadioButton1" />
的JavaScript:
$(function() {
$("#<%=RadioButtonList1.ClientID %> input:radio").change(function() {
$("#<%=Checkbox1.ClientID %>").next().text($("#<%=RadioButtonList1.ClientID %> input:radio:checked").val());
});
});
的RadioButtonList
将生成HTML一个<Table>
与RadioButtonList1
的ID
。 javascript选择器从该表格中选择所有单选按钮。虽然这将满足你的要求,但发送到服务器是另一回事。
正如我所说,我有复选框说“Checkbox1”text =“Text1”。我也有Radiobutton说“Radio1”和“Radio2”。现在默认Radio1被选中。但是当我检查Radio2时,Checkbox1的文本应该改为“Text2”。这是我想要的,但老实说,我是新来的ASP.net以及JavaScript ...请帮助 – Vijay 2011-12-30 07:00:29
您可以将AutoPostBack属性设置为true。这会将JavaScript添加到客户端,从而自动增加回发事件。
ASPX
<asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True"
oncheckedchanged="RadioButton1_CheckedChanged" />
<asp:CheckBox ID="CheckBox1" runat="server" />
的.cs
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
CheckBox1.Text = "some text...";
}
我想通过使用jQuery实现该任务将通过防止回传增加peformance。
<script>
$(function() {
$("input[name='<%=RadioButton1.ClientID %>']").change(function(){
$("#<%= CheckBox1.ClientID%>").next("label:first").text("New Text");
});
});
</script>
假设的单选按钮具有ID “RadioButton1”,复选框作为ID “CheckBox1”。
-1你在那里的第一个选择器将只抓取'RadioButton1.ClientID'类型的所有元素,所以如果ClientID是'测试',那么它会抓住所有像'
@Craig - 更新了我的答案。 – Bibhu 2011-12-29 15:38:10
真的不想在这里给你,但是...当你使用一个id选择器时,你错过了'#'符号,你有一个错字。此外,因为单选按钮是一个集合,所以设置id属性是不合适的...而是应该使用name属性并将您的选择器更改为类似'input [name ='<%= RadioButton1.ClientID%>']' 。如果你使用'#'标志向jQuery表明你想要一个带有指定'id'的元素,它将只会返回0或1个项目。 – Craig 2011-12-29 15:47:46
嘿AVD!很好的答案,但维杰希望改变复选框的文本而不是单选按钮。 – Bastardo 2011-12-29 14:06:43
@OkayGuy - 谢谢!我已经更新了它。 – adatapost 2011-12-29 14:10:00
@Vijay - 编辑您的文章和相关代码。 – adatapost 2011-12-30 06:56:10