Why doesn't ASP.net RadioButtonList onchange client side event trigger?

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

i have asp.net project, where i have a master-page on I include this line, to reference of the JS file

<script type="text/javascript" src="Scripts/HideDIV.js"></script> 

In the JS file i have this function:

function hideDiv() {     document.getElementById('div1').style.display = 'none';     document.getElementById('div2').style.display = 'none';      if (document.getElementById('RadioButtonTipoUser_0') != null) {         if (document.getElementById('RadioButtonTipoUser_0').checked) {             document.getElementById('div1').style.display = 'block';         }     }      if (document.getElementById('RadioButtonTipoUser_1') != null) {         if (document.getElementById('RadioButtonTipoUser_1').checked) {              document.getElementById('div2').style.display = 'block';          }     } } 

Basically i need on this RadioButtonList, call a function on Js "hideDiv()", when a select the one Button, one div hide pass to visible.

This code is in content.

<div>         <asp:RadioButtonList ID="RadioButtonTipoUser" runat="server" RepeatDirection="Horizontal" onchange="hideDiv()">              <asp:ListItem Selected="true" Value="1">Dome</asp:ListItem>             <asp:ListItem Value="2">Emp</asp:ListItem>             <asp:ListItem Value="3">Vet</asp:ListItem>         </asp:RadioButtonList>       </div>      <div id="div1" style="display:none">         <a>Charls</a>     </div>     <div id="div2" style="display:none"><a>Maris</a></div> </div> 

I make a debug and the error msg is

ReferenceError: hideDiv is not defined

how i make tho the onchange="hideDiv()" call the HideDiv() function?

Bests

回答1:

you use jquery for achieving your task

HTML

<div>         <asp:RadioButtonList ID="RadioButtonTipoUser" runat="server" RepeatDirection="Horizontal">              <asp:ListItem Selected="true" Value="1">Dome</asp:ListItem>             <asp:ListItem Value="2">Emp</asp:ListItem>             <asp:ListItem Value="3">Vet</asp:ListItem>         </asp:RadioButtonList>       </div>      <div id="div1" >         <a>Charls</a>     </div>     <div id="div2" ><a>Maris</a></div> 

JQUERY

 $(document).ready(function () {          $('#div1').hide();         $('#div2').hide();         $('#RadioButtonTipoUser_1').on('change', function () {              if ($(this).is(':checked')) {                 $('#div1').show();                 $('#div2').hide();             }         });         $('#RadioButtonTipoUser_2').on('change', function () {             alert("ok1");             if ($(this).is(':checked')) {                 $('#div1').hide();                 $('#div2').show();             }         });     }); 


回答2:

Our site was an intranet, and I had to turn off Display Intranet Sites in Compatibility View in Internet Explorer for onchange to work in RadioButtonList. It appears that ASP.NET actually adds a javascript event to the containing table that a RadioButtonList renders as, and IE in Compatibility View stops that working.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!