【问题标题】:Validation for RadioButtonList in Asp.net with JavaScript and Value Fetched from Database使用 JavaScript 和从数据库中获取的值验证 Asp.net 中的 RadioButtonList
【发布时间】:2020-05-27 14:10:15
【问题描述】:

我想在 RadioButtonList 上使用 JavaScript 验证,其中值由现有表 #tblGender 绑定,其中给定三个值 Male、Female 和 Others。我想表明,如果未选中任何按钮,则返回错误“请至少选择 1 个按钮”。

【问题讨论】:

  • 请向我们展示您的代码,以便您更好地理解您的问题。
  • 函数 chkGender() { var rb = document.getElementsByName("rblgender"); var isrbChecked = 假; for (var i = 0; i
  • 请在您的问题中添加代码

标签: asp.net database radiobuttonlist


【解决方案1】:

使用 Javascript 你可以这样:

function IsRadioButtonSelected() {
    var arrRadio = document.getElementsByName("NameOfRadioButton"),
        c = 0;
    return arrRadio.forEach(function(item, index) {
        item.checked && c++
    }), c > 0
}

然后这样称呼它:

if(IsRadioButtonSelected()){
    alert('Radio Button is Selected')
}
else{
    alert('Radio Button is not Selected')
}

...但是如果您使用 jQuery,它可以像这样简单得多:

if($("input[name=NameOfRadioButton]:checked").length>0){
    alert('Radio Button is Selected')
}else{
    alert('Radio Button is not Selected')
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    • 2012-03-13
    • 1970-01-01
    相关资源
    最近更新 更多