【问题标题】:Why a button inside an UpdatePanel doesn't execute a JQuery event after the first time为什么 UpdatePanel 中的按钮在第一次之后不执行 JQuery 事件
【发布时间】:2014-10-15 09:04:40
【问题描述】:

我有一个按钮(在 UpdatePanel 内),它打开一个弹出表单,允许我输入一些数据,然后提交执行一些功能(使用输入的新数据更新 UpdatePanel)并关闭弹出窗口:

<asp:HyperLink ID="hlCreateMsg" runat="server" NavigateUrl="JavaScript:void(0);" CssClass="linkOff" ClientIDMode="Static">Create New</asp:HyperLink>

<div id="popupContact">
    <a id="popupContactClose" title="Close Window">x</a>
    <h3>Add a New Message</h3>
    <div id="dvFirst" class="mainSecond">
        <div id="leftdiv3" class="leftdiv">Client: </div>
        <div id="rightdiv3" class="rightdiv"><asp:DropDownList ID="ddlCliNewMsg" AutoPostBack="false" ClientIDMode="Static" runat="server" CssClass="chosen-select" ></asp:DropDownList></div>
    </div>
    <div id="dvSecond" class="mainSecond">
        <div id="leftdiv4" class="leftdiv">Site: </div>
        <div id="rightdiv4" class="rightdiv"><asp:DropDownList ID="ddlSitNewMsg" AutoPostBack="false" ClientIDMode="Static" runat="server" CssClass="chosen-select"></asp:DropDownList></div>
    </div>
   <div id="dvThird" class="mainSecond">
        <div id="leftdiv5" class="leftdiv">Provider: </div>
        <div id="rightdiv5" class="rightdiv"><asp:DropDownList ID="ddlProNewMsg" AutoPostBack="false" ClientIDMode="Static" runat="server" CssClass="chosen-select"></asp:DropDownList></div>
    </div>
    <div id="dvFourth" class="mainFirst">
        <div id="leftdiv1" class="leftdivspec"><sup style="color: #FF0000; font-weight: bold;">*</sup>Message: </div>
        <div id="rightdiv1" class="rightdivspec"><asp:TextBox ID="tbMessage" ClientIDMode="Static" runat="server" TextMode="MultiLine" Columns="30" Rows="5"></asp:TextBox></div>
    </div>
    <div id="dvFifth" class="mainSecond">
        <div id="leftdiv2" class="leftdiv">Active?</div>
        <div id="rightdiv2" class="rightdiv"><asp:CheckBox ID="cbIsActive" ClientIDMode="Static" runat="server" /></div>
    </div>
    <div style="width: 96%; text-align: right; padding: 2%;">
        <asp:UpdatePanel runat="server" ID="upSubmit" ClientIDMode="Static" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="btnExport" ClientIDMode="Static" UseSubmitBehavior="false" OnClick="SubmitAdminMessage" />
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
</div>
<div id="backgroundPopup"></div>

脚本:

//CONTROLLING EVENTS IN jQuery
$(document).ready(function () {

    //LOADING POPUP
    //Click the button event!
    $("#hlCreateMsg").click(function (e) {
        e.preventDefault();
        //centering with css
        centerPopup();
        //load popup
        loadPopup();
    });
    $("#Create").click(function (e) {
        e.preventDefault();
        //centering with css
        centerPopup();
        //load popup
        loadPopup();
    });
    $("#btnSubmit").on('click', function (e) {
        alert('test');
        e.preventDefault();

        disablePopup();
    });

    //CLOSING POPUP
    //Click the x event!
    $("#popupContactClose").click(function () {
        disablePopup();
    });
    //Click out event!
    $("#backgroundPopup").click(function () {
        disablePopup();
    });
    //Press Escape event!
    $(document).keypress(function (e) {
        if (e.keyCode == 27 && popupStatus == 1) {
            disablePopup();
        }
    });
});


//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup() {
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#popupContact").fadeIn("slow");
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $("#backgroundPopup").fadeOut("slow");
        $("#popupContact").fadeOut("slow");
        $("#tbMessage").val('');
        $("#cbIsActive").attr("checked", false);
        popupStatus = 0;
    }
}

//centering popup
function centerPopup() {
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupContact").height();
    var popupWidth = $("#popupContact").width();
    //centering
    $("#popupContact").css({
        "position": "absolute",
        "top": windowHeight / 2 - popupHeight / 2,
        "left": windowWidth / 2 - popupWidth / 2
    });
    //only need force for IE6

    $("#backgroundPopup").css({
        "height": windowHeight
    });
}

所以第一次加载页面时,我点击hlCreateMsg 链接,弹出窗口,我可以输入数据并提交。弹出窗口关闭并更新UpdatePanel 还显示test 警报窗口。但是每次我打开弹出窗口(不刷新页面)时,提交按钮都会更新UpdatePanel,但它不会关闭弹出窗口,也不会显示test 警报。

我该如何解决这个问题?

更新:在查看了一些其他问题后,通过修改代码解决了这个问题:

$("#btnSubmit").live('click', function (e) {
        e.preventDefault();

        disablePopup();
    });

但这给了我一个错误:0x800a01b6 - Microsoft JScript runtime error: Object doesn't support this property or method

【问题讨论】:

  • .live() API 已被长期弃用。 Satisfy your search for knowledge here.
  • on 对我也不起作用:/
  • 你必须使用.on()的3参数版本。
  • .on( "click", "button", function(){});?
  • 对 - 你将处理程序挂在 &lt;body&gt; 或文档上,如下所示:$('body').on('click', '#btnSubmit', function(e) { ... }); - 它基本上做同样的事情 .live() 所做的,只是更好。

标签: javascript jquery html asp.net updatepanel


【解决方案1】:

如果您更改事件处理程序以使其使用来自&lt;body&gt; 的委托,那么当 DOM 更改时它们将继续工作。因此,例如:

$("body").on('click', "#btnSubmit", function (e) {
    alert('test');
    e.preventDefault();

    disablePopup();
});

处理函数真的不需要改变(通常)。我不确定您页面上的哪些元素被弹出机制覆盖,但可以(我很确定)对您的任何事件处理程序安全地进行上述转换。

【讨论】:

  • 哇成功了!!!对于 UpdatePanel 中的任何事件,我都应该更改为它!
  • @SearchForKnowledge 是的,通常我发现这是让复杂页面工作的最简单、最干净的方法。
  • 我从来不知道,但既然我现在知道......我会使用它。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-03
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多