【问题标题】:Why doesn't ASP.net RadioButtonList onchange client side event trigger?为什么 ASP.net RadioButtonList onchange 客户端事件不触发?
【发布时间】:2014-04-25 11:54:40
【问题描述】:

我有 asp.net 项目,我有一个母版页,我包含这一行,以引用 JS 文件

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

在 JS 文件中我有这个功能:

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';

        }
    }
}

基本上我需要在这个RadioButtonList上,调用Js“hideDiv()”上的一个函数,当a选择一个Button时,一个div隐藏传递到可见。

此代码在内容中。

<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>

我进行了调试,错误消息是

ReferenceError: hideDiv 未定义

我如何让 onchange="hideDiv()" 调用 HideDiv() 函数?

最佳

【问题讨论】:

  • 你检查过js文件是否加载正确吗?
  • 右击你的页面,现在去查看页面源代码。在哪里看到&lt;script type="text/javascript" src="Scripts/HideDIV.js"&gt;&lt;/script&gt;,然后单击 src。并检查它是否已连接
  • 是的,文件已加载。
  • 打开控制台并输入hideDiv(),看看会发生什么。如果这不起作用,则该功能由于某种原因超出范围 - 可能在外壳内定义。

标签: javascript jquery html asp.net


【解决方案1】:

你使用 jquery 来完成你的任务

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();
            }
        });
    });

【讨论】:

  • 嗨,我用 `$('#div1').hide(); $('#div2').hide();` 和 Jquery 运行。但其余代码不显示 div。
  • 你在哪里测试它?在您的项目或 js fiddle 上。
  • 好的,我正在将我的 asp:radio 按钮更新为输入类型收音机,这是jsfiddle.net/CYLcY/43
  • Ty,我改为文字。最好的
【解决方案2】:

我们的站点是一个 Intranet,我必须在 Internet Explorer 中关闭在兼容性视图中显示 Intranet 站点,onchange 才能在 RadioButtonList 中工作。似乎 ASP.NET 实际上将一个 javascript 事件添加到 RadioButtonList 呈现为的包含表中,并且兼容性视图中的 IE 停止了该工作。

【讨论】:

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