【问题标题】:Javascript ASP.net set radio button enabled to false in modal pop upJavascript ASP.net 在模式弹出窗口中将启用的单选按钮设置为 false
【发布时间】:2017-05-19 07:45:57
【问题描述】:

当通过单击按钮显示模态时,如何将启用的单选按钮默认设置为 false。单击按钮时,它不会禁用单选按钮。我该如何正确地做到这一点。谢谢


我的 javascript 函数

function btnDefaultRadios() {

document.getElementById('<%= rdPermanent.ClientID %>').disabled = true;
            document.getElementById('<%= rdNonPermanent.ClientID %>').disabled = true;
            document.getElementById('<%= rdAdministrative.ClientID %>').disabled = true;
            document.getElementById('<%= rdTechnical.ClientID %>').disabled = true;
            document.getElementById('<%= rdWithhRata.ClientID %>').disabled = true;
            document.getElementById('<%= rdContractual.ClientID %>').disabled = true;
            document.getElementById('<%= rdCasual.ClientID %>').disabled = true;
        }

我的按钮触发模态

<asp:Button ID="btnAdd" runat="server" SkinID="button" Text="Add New Position" Width="168px"  OnClientClick="btnDefaultRadios(this);"/>

我的模式 - 我的控件在面板内

<cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="Label1"
                PopupControlID="pnladd" BackgroundCssClass="modalBackground" OnOkScript="OkButtonClick()">
            </cc1:ModalPopupExtender>

【问题讨论】:

  • 你检查过浏览器的调试控制台是否有错误吗?也许添加一些 console.log 语句来查看它是否到达被调用的函数。根据脚本加载的时间,它可能会抛出 js 异常。
  • 您好,调试器确实读取了我的行,但是它仍然没有禁用。请帮忙

标签: javascript asp.net


【解决方案1】:

一个快速的 jsfiddle https://jsfiddle.net/8Lq73hye/ 表明如果你的 javascript 被执行,它应该可以工作。您可以在浏览器上的开发人员工具中设置一个断点,然后单步执行代码以查看禁用逻辑是否确实有效。

可能发生的情况是您的 js 正在执行,但随后单击按钮立即导致回发。如果您在本地计算机上,这可能会在您不知情的情况下迅速发生。

为防止意外回发,请确保您的 btnDefaultRadios 函数返回 false

function btnDefaultRadios() {
    document.getElementById('<%= rdPermanent.ClientID %>').disabled = true;
    document.getElementById('<%= rdNonPermanent.ClientID %>').disabled = true;
    document.getElementById('<%= rdAdministrative.ClientID %>').disabled = true;
    document.getElementById('<%= rdTechnical.ClientID %>').disabled = true;
    document.getElementById('<%= rdWithhRata.ClientID %>').disabled = true;
    document.getElementById('<%= rdContractual.ClientID %>').disabled = true;
    document.getElementById('<%= rdCasual.ClientID %>').disabled = true;
    return false;
}

return false 将停止回帖。一个常见的错误是忘记在 OnClientClick 调用中返回该函数结果,因此您还必须将 OnClientClick 更改为

<asp:Button ID="btnAdd" runat="server" ...  OnClientClick="return btnDefaultRadios(this);"/>

编辑:

aspx 的简化工作示例:

<%@ Page Language="C#" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Test Client Side JS on Button Click</title>
    <script>
        function btnDefaultRadios()
        {
            document.getElementById('<%= rdPermanent.ClientID %>').disabled = true;
            document.getElementById('<%= rdNonPermanent.ClientID %>').disabled = true;
            return false;
        }

        function OkButtonClick(){}
    </script>
    <style>
        #pnladd
        {
            background-color: #fff;
        }
         .modalBackground
         {
             background-color: #666;
             background-color: rgba(200,200,200,0.75);
         }
    </style>
</head>
<body>
    <form id="HtmlForm" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <asp:Label runat="server" ID="Label1">label1</asp:Label>
            <asp:Button ID="btnAdd" runat="server" Text="Add New Position" 
                Width="168px" OnClientClick="return btnDefaultRadios(this);" />
            <asp:Panel runat="server" ID="pnladd" Width="300" Height="300">
                <asp:RadioButton runat="server" ID="rdPermanent" Text="Permanent"/>
                <asp:RadioButton runat="server" ID="rdNonPermanent" Text="Non Permanent"/>
            </asp:Panel>
            <cc1:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="btnAdd"
                PopupControlID="pnladd" BackgroundCssClass="modalBackground" OnOkScript="OkButtonClick()">
            </cc1:ModalPopupExtender>
        </div>
    </form>
</body>
</html>

【讨论】:

  • 我试过你的代码先生。但是,模态不会弹出
  • 您上面的原始问题详细信息显示模态已连接到“Label1”的目标控件,因此更改添加按钮的 onclientclick 行为不应影响模态的显示。您的意思是将 TargetControlID 设置为使用 btnAdd 而不是 label1?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-30
  • 1970-01-01
  • 2014-01-06
  • 1970-01-01
  • 2018-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多