如何在C#中选中或不选中Html单选按钮? [英] How Can I Check Html Radio Button Is Selected Or Not In C#

查看:186
本文介绍了如何在C#中选中或不选中Html单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已动态创建三个html单选按钮



在C#中我需要检查所有单选按钮是否被选中



< input type =   radio name =  答案 id =  < span class =code-string> radiodynamic1 >  
< input type = radio name = 答案 id = radiodynamic2 >
< input type = radio name = 答案 id = RA diodynamic3 >







我有点数



  for  int  i =  1 ; i <  =  3 ; i ++)
{
if (Request [ radiodynamic + i]!= null
{
// 这里如何选中是否选中单选按钮

}

}





但它无法正常工作



如何检查收音机是否选择了按钮

请帮助我

解决方案

 属性下方的添加到单选按钮,然后检查已检查属性 代码behin 

< input type = radio id = radioExample1 runat = server />

// 代码隐藏
if (radioExample1.Checked)
{
// 写你的代码在这里
}



//让我知道它是否对你有帮助


你应该为每个按钮附加一个Javascript onclick事件处理程序:



< input type =    radio name =  答案  onclick =   answer_click(this); value =   1> 
< input type = radio name = 回答 onclick = answer_click(this); value = 2>
< input type = radio name = 回答 onclick = answer_click(this); value = 3>





某处,定义Javascript函数 answer_click



< pre lang =Javascript> function answer_click(radio_button)
{
switch (radio_button.value)
{
case 1
代码块 1
break ;

case 2
代码块 2
break ;

case 3
代码块 3
break ;

默认
代码块 for 当radio_button .value不是 1 2 ,或 3
break ;
}


I have create dynamically three html radio button

In C# I need check all radio buttons are selected or not

<input type="radio" name="Answer" id="radiodynamic1">
<input type="radio" name="Answer" id="radiodynamic2">
<input type="radio" name="Answer" id="radiodynamic3">




I have count

for (int i = 1; i <= 3; i++)
              {
                  if (Request["radiodynamic" + i] != null)
                  {
// here how can i check radio button is selected or not

}

}



but it will not work properly

how can i check if radio buttons are selected or not
Please help me

解决方案

add below property to the radio buttons and check the checked property in code behin

<input type="radio" id="radioExample1" runat="server" />

//code behind 
if (radioExample1.Checked)
{
     //write your code here
}


//let me know whether it helps you or not


You should attach a Javascript onclick event handler to each button:

<input type="radio" name="Answer" onclick="answer_click(this);" value="1">
<input type="radio" name="Answer" onclick="answer_click(this);" value="2">
<input type="radio" name="Answer" onclick="answer_click(this);" value="3">



Somewhere, define the Javascript function answer_click:

function answer_click ( radio_button )
  {
  switch ( radio_button.value )
    {
    case 1:
      code block 1
      break;

    case 2:
      code block 2
      break;

    case 3:
      code block 3
      break;

    default:
      code block for when radio_button.value is not 1, 2, or 3 
      break;
    }


这篇关于如何在C#中选中或不选中Html单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆