简体   繁体   中英

Validate RadioButtonGroup Client Side

<asp:RadioButton GroupName="EndorsementType" runat="server" ID="rdoAddProperty" Text="Add Property to TIV" />
<asp:RadioButton GroupName="EndorsementType" runat="server" ID="rdoRemoveProperty" Text="Remove Property from TIV" />
<asp:RadioButton GroupName="EndorsementType" runat="server" ID="rdoChangeProperty" Text="Change Property Values" />

I was thinking about implementing a custom validator , and using a client JavaScript function to reference the RadioButton ID, (I'm using web forms, not mvc),

something like.. 
 if(document.getElementById(<%= rdoAddProperty.ClientId %>).checked == true) && ...

Anyone know of a way to do it without knowing the clientId?

If your radios are contained within something like a DIV, and because your asp:RadioButtons will render as HTML inputs, you could do something like:

<script type="text/javascript" language="javascript">
    function Validate()
    {
        var l_elemsRadios = document.getElementById("MyRadios").getElementsByTagName("input");

        if (l_elemsRadios == null)
            return;

        for (var i = 0; i < l_elemsRadios; i++)
        {
            // validate l_elemsRadios[i] through l_elemsRadios[n]
        }
    }
</script>


<div id="MyRadios">
    <input type="radio" name="EndorsementType" value="Remove Property from TIV" >Remove Property from TIV
    .
    .
    .
</div>

您必须将客户ID放入表单中,就像使用INamingContainer一样,您的ID可以相对于存储在服务器上的其他信息进行更改。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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