简体   繁体   English

ASP.net使用javascript在内容页面上获取RadioButtonList值

[英]ASP.net getting RadioButtonList value on content page with javascript

I'm having a problem with my JavaScript code having moved it over to a content page in ASP. 我的JavaScript代码将其移至ASP中的内容页面时遇到问题。

It worked perfectly on a stand alone page. 它在独立页面上完美运行。

<script type="text/javascript">
        function validateQ12(oSrc, args) {
            var rbtnList = document.getElementsByName('rbtn12');
            var noClicked = false;
            for (var i = 0; i < rbtnList.length; i++) {
                if (rbtnList[i].checked) {
                    noClicked = rbtnList[i].value == "No";
                    break;
                }
            }
            if (noClicked) {
                args.IsValid = true;                   
            }
            else
                args.IsValid = args.Value.trim().length > 0;                   
        }
    </script> 

I have gone through the html on both pages, on the one that worked had 我浏览了两个页面上的html,在那个页面上

<input id="rbtn12_1" type="radio" name="rbtn12" value="No" checked="checked" onclick="Q12iNo();">

The new one inside a content page has 内容页面中的新内容有

<input id="MainContent_rbtn12_1" type="radio" name="ctl00$MainContent$rbtn12" value="No" checked="checked" onclick="Q12iNo();">

Clearly the name change must be causing the problem. 显然,名称更改一定是引起问题的原因。 How do I reference this new name in my javascript code? 如何在我的JavaScript代码中引用这个新名称?

To get the rendered element name use the following: 要获取渲染的元素名称,请使用以下命令:

<%= rbtn12.UniqueID %>

However in your case this may still not work as it looks like you need to loop through each radio button item. 但是,根据您的情况,这可能仍然无法正常工作,因为您似乎需要遍历每个单选按钮项。

See the following post: 请参阅以下帖子:

Find out if radio button is checked with JQuery? 找出JQuery是否选中了单选按钮?

Which shows how to determine if a radio button collection has a selected value. 它显示了如何确定单选按钮集合是否具有选定值。

Can you possibly add a className to your control and use getElementByClassName? 您是否可以将className添加到控件中并使用getElementByClassName?

<input id="MainContent_rbtn12_1" type="radio" name="ctl00$MainContent$rbtn12" value="No" checked="checked" onclick="Q12iNo();" CssClass="Radio">

<script type="text/javascript">
        function validateQ12(oSrc, args) {
            var rbtnList = document.getElementsByClassName('Radio');
            var noClicked = false;
            for (var i = 0; i < rbtnList.length; i++) {
                if (rbtnList[i].checked) {
                    noClicked = rbtnList[i].value == "No";
                    break;
                }
            }
            if (noClicked) {
                args.IsValid = true;                   
            }
            else
                args.IsValid = args.Value.trim().length > 0;                   
        }
    </script> 

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

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