我的jquery选择器在内容页面中的更新面板后不起作用

问题描述 投票:0回答:1

我有一个内容页面,我在asp:updatepanel之后写了一些jquery选择器,第一次,当页面加载$(document).ready(function()工作正常,但在回发后,选择器不再工作,任何方式确实存在解决这个问题??

<asp:content id="Content1" contentplaceholderid="contentplaceholder1" runat="server">       
     <asp:UpdatePanel ID="UpdatePanel1" runat="server">
         <ContentTemplate>
              <asp:TextBox ID="txtdate" runat="server" CssClass="persianDTP" ></asp:TextBox>
                           <!-- some code --> 

           </ContentTemplate>
    </asp:UpdatePanel>

    <script>
                    $(document).ready(function () {
                        $('.PersianDTP').datepicker({
                            showOn: 'button',
                            buttonImage: 'calendar.png',
                            dateFormat: 'yy/mm/dd',
                            //appendText: ' (yy/mm/dd)',
                            changeMonth: true,
                            changeYear: true,
                            //selectOtherMonths: true,
                            //showOtherMonths: true,
                            showStatus: true,
                            showButtonPanel: true,
                            buttonImageOnly: true,
                            buttonText: 'Choose a Date',
                            onClose: function () {
                                this.focus();
                            }
                        });

                        jQuery(function ($) {
                            $(".PersianDTP").mask("9999/99/99");
                        });  

                    });
     </script>
</asp:content>
jquery asp.net content-pages
1个回答
0
投票

所有JQuery插件都应用于HTML页面的页面加载事件,或者换句话说,文档就绪事件,当整个页面或文档完全在浏览器中呈现时触发。但是当控件在UpdatePanel内部并且发生部分PostBack时,唯一的Ids由jQuery分配丢失,因此插件停止工作,每次UpdatePanel的异步请求或部分PostBack完成后,您可以重新应用jQuery插件:您需要在回发时重新创建Jquery代码。 示例代码:)

<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <script type="text/javascript">
        $(document).ready(function () {
            $(<%=lstBoxTest.ClientID%>).SumoSelect({ selectAll: true });
        });

        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
        function EndRequestHandler(sender, args) {
            //Binding Code Again
            $(<%=lstBoxTest.ClientID%>).SumoSelect({ selectAll: true });
        }
    </script>
© www.soinside.com 2019 - 2024. All rights reserved.