客户端单选按钮列表验证

Client-side RadioButtonList validation

本文关键字:验证 列表 单选按钮 客户端      更新时间:2023-09-26
 function IsChecked()
     {
    var rblActive = document.getElementById("<%=rblActive.ClientID %>");
    var item = rblActive.getElementsByTagName("input");
    var IsItemChecked = false;
    for (var i = 0; i < item.Length; i++)
     {
        if (item[i].checked) 
        {
        IsItemChecked = true;
        }
      }
        if (IsItemChecked == false) 
          {
          alert("Check Yes or No");
          rblActive.focus();
          return false;
           }
          return true;
      }

这是我尝试过的代码。当控件进入 for 循环时,即使选中或未选中单选按钮列表中的项,它也会直接出来而不执行任何操作。

你也可以

用jQuery做同样的事情。

function ValidateControls() {
    var count = 0;
    $("input[type=radio]").each(function () {
        if ($(this).attr('checked')) {
            count++;
        }
    });
    if (count > 0) {
        return true;
    }
    else {
        alert("No Row Selected");
        return false;
    }
}