本文介绍了在asp.net中的单选列表控件中进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何应用JavaScript验证来检查是否选中了至少一个单选按钮列表选项.

(预先感谢.)

How i can apply javascript validation to check atleast one radiobutton list option is selected.

(Thanks in advance.)

推荐答案

<script type="text/javascript" mode="hold" />function getName1()
{
    var rbCount = 0;
    var rbtable = document.getElementById('rblistInterests'); // Table Name => Your Radiobuttonlist ID
    var rbtr = rbtable.getElementsByTagName('tr');
    for(var i=0; i< rbtr.length;i++)
    {
        var rbtd = rbtr[i].getElementsByTagName('td');
        for(var j=0; j < rbtd.length; j++)
        {
            var rbinput = rbtd[j].getElementsByTagName('input');
            var rblabel= chktd[j].getElementsByTagName('label');
            for(k=0;k < rbinput.length;k++)
            {
                var rbopt = rbinput[k];
                if(rbopt.checked)
                {
                  return true; // At least one readio button selected.
                }
            }
        }
    }
    return false; // no one readiobutton selected.
}&lt;/script&gt;



我有这种单选按钮



I have radio button this way

<asp:RadioButtonList runat="server" ID="rblistInterests" RepeatColumns="3" RepeatDirection="Vertical" RepeatLayout="Table">
        </asp:RadioButtonList>



现在,按下一个按钮并执行onclientclick事件,如果选择了至少一个readio按钮,则调用此函数,然后返回true,否则返回false.



Now take one button and take onclientclick event and call this function if atleast one readio button is selected then it is return true otherwise it is return false.


<script type="text/javascript">
var wasOkPressed = false;
// Create an array of your checked controls
var controlArray = [ '<%= RadioButtonList1.ClientID %>'];
function validate() {
    wasOkPressed = false;
    // Loop through each control
    for(var i = 0; i < controlArray.length; i ++) {
        if (!wasOkPressed ) { // If ok was not pressed keep validating
            if (!validateRBL(controlArray[i])) {
                // If control fails to validate return false
                return false;
            }
        }
        wasOkPressed=true;
    }
    return true;
}
function validateRBL(RBLid)
{
 var listItemArray = document.getElementsByName(RBLid);
 var isItemChecked = false;
     for (var i=0; i<listItemArray.length; i++)
     {
        var listItem = listItemArray[i];
          if ( listItem.checked )
          {
           //alert(listItem.value);
           isItemChecked = true;
          }
     }
     if ( isItemChecked == false )
     {
         var answer = confirm ("Please confirm selection...")
         if (answer)
         return true;
         else
         return false;
     }
 return true;
}
</script>







<asp:RadioButtonList ID="RadioButtonList1" runat="server">
      <asp:ListItem Value="1">Check 1</asp:ListItem>
      <asp:ListItem Value="2">Check 2</asp:ListItem>
  </asp:RadioButtonList>
  <input type="button" value="Validate Radio List" onclick="validate()" />



如果我误解了您的问题,请随时纠正我.
希望以上信息对您有所帮助.如果您还有其他疑问,请告诉我.



If i misunderstand your question, please feel free to correct me.
I hope the above information will be helpful. If you have more concerns, please let me know.



这篇关于在asp.net中的单选列表控件中进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 21:14