Bootstrap 弹出窗口中的日期选择器在事件显示触发时清除其他输入

Bootstrap datepicker in popover clear other inputs when event show fired

我在弹出框 window 中有一个表单,在这个表单中我使用日期选择器 bootstrap 输入。我在使用日期选择器输入之前填写所有输入,但是当我打开日期选择器时,所有输入都被清除。 有人可以帮忙吗?

    $('.datepicker').datepicker({
        format: 'dd.mm.yyyy',
        language: 'ru',
        autoclose: true
    });
<div class="modal fade" id="taskform" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
 <div class="modal-dialog" role="document">
  <div class="modal-content">
   <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
    <h4 class="modal-title" id="myModalLabel">Создать задачу</h4>
   </div>
   @using (Html.BeginForm(Model.ActionName, Model.ControllerName, FormMethod.Post, new { id = "task_form" }))
   {
    <div class="modal-body">
     <div class="form-group">
      @Html.HiddenFor(m => m.CampaignId)
      @Html.HiddenFor(m => m.AuthorId)

      @Html.LabelFor(x => x.Title)
      @Html.TextBoxFor(x => x.Title, new { @class = "form-control form-input" })
     </div>
      
     <div class="form-group">
      <br />
      @Html.LabelFor(x => x.Description)
      @Html.TextAreaFor(x => x.Description, new { @class = "ckeditor form-control form-input", rows = "3" })
      <br />
     </div>
     <div class="form-group">
      @Html.LabelFor(x => x.DeadLine)
      @Html.TextBoxFor(x => x.DeadLine, new { @class = "form-control form-input datepicker", placeholder = "Дедлайн" })
      <br />
     </div>
    </div>
    <div class="modal-footer" style="text-align: left;">
     <button type="button" id="task-submit" class="btn btn-primary" style="float: right;">Создать</button>
     @Html.DropDownListFor(m => m.Performers, Model.AllPerformers.Select(item => new SelectListItem()
     {
      Text = item.FullName,
      Value = Convert.ToString(item.Id)
     }), new { @class = "form-control select2 ", style = "", multiple = "multiple", placeholder = "Добавьте исполнителей" })
    </div>
   }
  </div>
 </div>
</div>

我尝试重现您的问题,但单击日期选择器时我的表单没有清除其他输入。

查看:

<button type="button" class="btn btn-primary" id="btnAdd" data-target="#modalTaskForm" aria-label="Left Align">
  <span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add Task
</button>
<!-- Add Modal -->
<div id="modalTaskForm" class="modal fade" role="dialog">
  <div class="modal-dialog modal-sm">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Task Form</h4>
      </div>
      <div class="modal-body">
        @using (Html.BeginForm("PopOver","Home", FormMethod.Post, new { id = "task_form" }))
        {
            <div class="modal-body">
                <div class="form-group">
                    @Html.HiddenFor(m => m.CampaignId)
                    @Html.HiddenFor(m => m.AuthorId)

                    @Html.LabelFor(x => x.Title)
                    @Html.TextBoxFor(x => x.Title, new { @class = "form-control form-input" })
                </div>

                <div class="form-group">
                    <br />
                    @Html.LabelFor(x => x.Description)
                    @Html.TextAreaFor(x => x.Description, new { @class = "ckeditor form-control form-input", rows = "3" })
                    <br />
                </div>
                <div class="form-group">
                    @Html.LabelFor(x => x.DeadLine)
                    @Html.TextBoxFor(x => x.DeadLine, new { @class = "form-control form-input datepicker", placeholder = "Дедлайн" })
                    <br />
                </div>
            </div>
            <div class="modal-footer" style="text-align: left;">
                <button type="button" id="task-submit" class="btn btn-primary" style="float: right;">Создать</button>
            </div>
        }           
      </div>
    </div>
  </div>
</div>
<!-- /.modal -->

脚本:

<script>
    $(document).ready(function () {
        $('.datepicker').datepicker({
            format: 'dd.mm.yyyy',
            language: 'ru',
            autoclose: true
        });

    });

    $(document).on('click', '#btnAdd', function () {
        $("#modalTaskForm").modal();
    });
</script>

万一有人还需要它:

使用显示和隐藏事件代替显示和隐藏并不总是可行的。更好的解决方法是检查事件命名空间:

modal.on('show.bs.modal', function(e) {
    if (e.namespace === 'bs.modal') {
        //
    }
});

来自 https://github.com/uxsolutions/bootstrap-datepicker/issues/978

试试这个代码

$("#my-datepicker").datepicker().on('show.bs.modal', function(event) {
    // prevent datepicker from firing bootstrap modal "show.bs.modal"
    event.stopPropagation();
});