简体   繁体   English

使用JavaScript在asp.net中验证radiobuttonlist

[英]validation of radiobuttonlist in asp.net using javascript

i have to validate a radio button using javascript. 我必须使用JavaScript验证单选按钮。 it should show a message if none of the option is selected. 如果未选择任何选项,它将显示一条消息。

my radio button code is: 我的单选按钮代码是:

<asp:RadioButtonList ID="welldata" runat="server" RepeatDirection="Horizontal" Width="100px">
    <asp:ListItem></asp:ListItem>
    <asp:ListItem></asp:ListItem>
    </asp:RadioButtonList>

and the submit button is: 提交按钮是:

<asp:Button ID="next2" runat="server" Text=">>" Width="46px" 
        OnClientClick="return next2()"/>

the corresponding javascript function is : 相应的javascript函数是:

        function next2() {

        var listItemArray = document.getElementById('<%=welldata.ClientID %>');
        var isItemChecked = false;
        var length1 = listItemArray.length;

        for (var i=0; i<length1; i++)
          {
              var listItem = listItemArray[i];
              document.write(listItem);

             if ( listItem.checked )
                 {
               alert(listItem.value);
               isItemChecked = true;
                 }
           }

            if ( isItemChecked == false )
              {
              alert('Nothing is checked for welldata!');

              return false;
                 }

             return true;
    }

while debugging i noticed thaat function is executed but doesn't enter the for loop. 在调试时,我注意到thaat函数已执行,但没有进入for循环。 also i tried 我也尝试过

        document.write(isItemChecked);
        document.write(listItemArray);
        document.write(length1);

and the output was : 输出为:

false [object HTMLTableElement] undefined 假[object HTMLTableElement]未定义

You are using a RadiobuttonList which is rendered as table per default. 您正在使用一个RadiobuttonList,默认情况下它以表格形式呈现。 If you select the dom element by id document.getElementById('<%=welldata.ClientID %>') then you are selecting the table and not the RadioButtonList. 如果通过id document.getElementById('<%=welldata.ClientID %>')选择dom元素,则选择的是表而不是RadioButtonList。 If you want to select the radio buttons then you have to loop through the childNodes of listItemArray to get the radio buttons. 如果要选择单选按钮,则必须遍历listItemArray的childNodes以获取单选按钮。 Alternatively you could use jquery to select the radio buttons, as their ids will start with the id of your radio button list (they will look like welldata_0 or welldata_1 ). 另外,您可以使用jquery选择单选按钮,因为它们的ID将以单选按钮列表的ID开头(它们看起来像welldata_0welldata_1 )。

This line of jquery code will get the your radio buttons 这行jQuery代码将使您的单选按钮

var listItemArray = $("input[id^='welldata']")

The wellData RadioButtonList ASP.NET server control will render in the browser as a table with a number of input type="radio" controls under it, each with the same name . wellData RadioButtonList ASP.NET服务器控件将在浏览器中呈现为表,并在其下具有多个input type="radio"控件,每个控件都具有相同的name

So, you need to get the input tags inside the table tag first: 因此,您需要先将input标签放入table标签中:

var wellData = document.getElementById('<%=welldata.ClientID %>');
var listItemArray = wellData.getElementsByTagName('input');

This is, of course, if you are doing this manually for some odd reason. 当然,如果您出于某种奇怪的原因手动执行此操作,则是这样。 You can do it automatically with a RequiredFieldValidator control. 您可以使用RequiredFieldValidator控件自动执行此操作。

<asp:RadioButtonList ID="welldata" runat="server" RepeatDirection="Horizontal" Width="100px">
    <asp:ListItem></asp:ListItem>
    <asp:ListItem></asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="rfvWellData" runat="server" ControlToValidate="wellData" Display="Dynamic" ErrorMessage="Pick yourself some well data" />

Try this: 尝试这个:

function next2() {

        if(document.getElementById('<%=welldata.ClientID %>').checked==false)
{
alert('Nothing is checked for welldata!');
return false;
}
return true;
}
                   or 

Use RequiredFieldValidator 使用RequiredFieldValidator

<asp:RequiredFieldValidator   
            ID="ReqiredFieldValidator1"  
            runat="server"  
            ControlToValidate="welldata"  
            ErrorMessage="Select your welldata!"  
            >  

Check these below links 检查以下这些链接

http://asp-net-example.blogspot.in/2009/02/aspnet-requiredfieldvalidator-example.html http://asp-net-example.blogspot.in/2009/02/aspnet-requiredfieldvalidator-example.html

http://www.itechies.net/tutorials/jscript/jsexample.php-pid-jform.htm#rb http://www.itechies.net/tutorials/jscript/jsexample.php-pid-jform.htm#rb

http://www.codeproject.com/Questions/499602/RequiredplusFieldplusValidatorplusForplusRadioplus http://www.codeproject.com/Questions/499602/RequiredplusFieldplusValidatorplusForplusRadioplus

var rblItems = document.getElementById('<%=radioBtnListId.ClientID %>').getElementsByTagName('input');

var checkedItems = true;
for (var i = 0; i < rblItems.length; i++) {
    if (rblItems[i].checked) {
        checkedItems = true;
        break;
    }
    else {
        checkedItems = false;
    }
}

if (!checkedItems) {//Hence No items checked
   alert("Nothing is checked");
   return false; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM