简体   繁体   English

JavaScript中弹出的模态无效

[英]Modal pop up in JavaScript won't work

I am trying to make simple pop up window using javascript with ASP.NET web forms. 我正在尝试使用JavaScript和ASP.NET Web表单来制作简单的弹出窗口。

I have following code: 我有以下代码:

<%@ Register Src="~/Controls/BankInformation.ascx" TagName="BankInformation" TagPrefix="ABS" %>

    <script type="text/javascript">
        $(document).ready(function () {
            var bankInformation = $('#<%=bankInformation.ClientID%>');
            var sameAsMerchantBankCheckbox = $('#<%=chkSameAsMerchantBank.ClientID%>');
            function OpenDirectDebitDialog(dialogID) {
                $("#" + dialogID).modal();
            }

            function ValidateAndCloseDirectDebitDialog(validationGroup, dialogID) {
                var pageValid = Page_ClientValidate(validationGroup);
                if (pageValid)
                    $("#" + dialogID).modal("hide");
            }

            $(sameAsMerchantBankCheckbox).change(function () {
                OpenDirectDebitDialog(bankInformation);
            });
        });
    </script>

    <asp:CheckBox runat="server" ID="chkDirectDebit" />
    <asp:Label runat="server" AssociatedControlID="chkSameAsMerchantBank" ID="txtDirectDebit" meta:resourcekey="lblDirectDebit"></asp:Label>
    <asp:CheckBox runat="server" ID="chkSameAsMerchantBank" AutoPostBack="True" Checked="True" />
    <asp:Label runat="server" AssociatedControlID="txtSameAsMerchantBank" ID="txtSameAsMerchantBank" meta:resourcekey="lblSameAsMerchantBank"></asp:Label>

    <div id="bankInfoDialog" runat="server" class="modal hide fade" tabindex="-1" role="dialog" aria-hidden="true">
        <ABS:BankInformation runat="server" ID="bankInformation" />
    </div>

On deselecting chkSameAsMerchantBank checkbox event should fire and OpenDirectDebitDialog function should be invoked. 在取消选择chkSameAsMerchantBank复选框事件应触发并应调用OpenDirectDebitDialog函数。 But all that happens is page refresh(auto post back I guess). 但是所有发生的都是页面刷新(我想是自动回发)。 No errors or anything in browser console. 在浏览器控制台中没有错误或任何错误。 Does anybody sees what I'm doing wrongly? 有人看到我做错了吗? Can I even invoke hidden form on modal like this? 我甚至可以像这样在模式上调用隐藏形式吗?

You don't have to re create a jQuery id selector again in the function as: 您不必在函数中重新创建jQuery id选择器,如下所示:

 $("#" + dialogID).modal();

because the passed argument is already a jQuery object. 因为传递的参数已经是jQuery对象。 so just remove the "#"+ from it. 因此只需从中删除"#"+


Why? 为什么? see below: 见下文:

As this var is a jQuery object: 由于此var是一个jQuery对象:

var bankInformation = $('#<%=bankInformation.ClientID%>'); // id selector jq object.

You don't have to make it a id selector of it again in the function: 您不必在函数中再次使其成为id选择器:

var sameAsMerchantBankCheckbox = $('#<%=chkSameAsMerchantBank.ClientID%>');// id selector.

which is passed in the function on change event: 在更改事件中传递给函数:

$(sameAsMerchantBankCheckbox).change(function () { // <----this will do.
    OpenDirectDebitDialog(bankInformation); // <----passed the jQuery selector object here.
});    

so you just need to do this: 所以你只需要这样做:

function OpenDirectDebitDialog(dialogID) {
   dialogID.modal(); // it would suffice.
   // or $(dialogID).modal(); 
}

You have to do 你必须做

var sameAsMerchantBankCheckbox = $('#<%=chkSameAsMerchantBank.ClientID%>');

in your code then access checkbox Id 在您的代码中,然后访问复选框ID

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

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