当页面回发 ASP.NET 时,时间选择器 javascript 停止工作

A timepicker javascript stops working when the page does postback ASP.NET

我的 page.aspx 中有一个更新面板,在更新面板中我有带有 bootstrap 时间选择器的文本框,我举了一个例子:

      <asp:UpdatePanel runat="server" id="upd1">
                <ContentTemplate>
 <div class="form-group">
                    <div class="row col-md-12 col-md-offset-0">
                        <label class="control-label">Monday</label>                            
                    </div>
                </div>

                <div class="form-group col-md-3">
                    <div class="input-group bootstrap-timepicker timepicker input-sm">
                        <asp:TextBox runat="server" ID="txtFecIni_1" class="form-control input-small "></asp:TextBox>
                        <span class="input-group-addon "><i class="fa fa-clock-o"></i></span>
                    </div>
                </div>

                <div class="form-group col-md-3">
                    <div class="input-group bootstrap-timepicker timepicker input-sm">
                        <asp:TextBox runat="server" ID="txtFecFin_1" class="form-control input-small "></asp:TextBox>
                        <span class="input-group-addon "><i class="fa fa-clock-o"></i></span>
                    </div>
                </div>

              </ContentTemplate>
                </asp:UpdatePanel>

在我页面的 </ form> 下,我添加了这个 javascript 代码:

       <script type="text/javascript">
        window.onload = function () {
  $('#txtFecIni_1').timepicker({
                minuteStep: 30,
                defaultTime: false,
                showMeridian: false
            });

            $('#txtFecFin_1').timepicker({
                minuteStep: 30,
                defaultTime: false,
                showMeridian: false
            });
}

    </script>

    <script src="js/jquery-2.2.2.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
        <script src="js/bootstrap-timepicker.js"></script>

时间选择器可以正常工作,直到对象被自动回传。我该如何解决这个问题? 谢谢。

我是这样解决问题的:

我把window.onload = function ()换成function pageLoad() 像那样:

之前:

     <script type="text/javascript">
        window.onload = function () {
  $('#txtFecIni_1').timepicker({
                minuteStep: 30,
                defaultTime: false,
                showMeridian: false
            });

            $('#txtFecFin_1').timepicker({
                minuteStep: 30,
                defaultTime: false,
                showMeridian: false
            });
}

    </script>

之后:

 <script type="text/javascript">
          function pageLoad() {
  $('#txtFecIni_1').timepicker({
                minuteStep: 30,
                defaultTime: false,
                showMeridian: false
            });

            $('#txtFecFin_1').timepicker({
                minuteStep: 30,
                defaultTime: false,
                showMeridian: false
            });
}

    </script>

我读到页面加载在每次回发时都会被调用。 希望这会对某人有所帮助

这是由于 asp.net 控件 here is explaination

的回发

使用Sys.WebForms.PageRequestManage

你的代码将是这样的..

<script>
                jQuery(document).ready(function () {

                       $('#txtFecIni_1').timepicker({
                    minuteStep: 30,
                    defaultTime: false,
                    showMeridian: false
                });

                $('#txtFecFin_1').timepicker({
                    minuteStep: 30,
                    defaultTime: false,
                    showMeridian: false
                    });
              });
                var prm = Sys.WebForms.PageRequestManager.getInstance();

                prm.add_endRequest(function () {


                       $('#txtFecIni_1').timepicker({
                    minuteStep: 30,
                    defaultTime: false,
                    showMeridian: false
                });

                $('#txtFecFin_1').timepicker({
                    minuteStep: 30,
                    defaultTime: false,
                    showMeridian: false
                    });
                });
            </script>