2

I have a form in popover window, in this form I have input with datepicker bootstrap. I fill all inputs before input with datepicker, but when I open datepicker all inputs clear. Can anyone help?

    $('.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>
Pavel Kononenko
  • 318
  • 3
  • 15
  • Possible duplicate of https://stackoverflow.com/questions/23311222/how-do-you-prevent-bootstrap-datepicker-from-clearing-existing-value-from-input – Vivek Buddhadev Jan 21 '18 at 06:37

3 Answers3

9

In case someone still need it :

Using shown and hidden events instead of show and hide is not always possible. A better workaround is to check the event namespace:

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

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

Hayanno
  • 182
  • 2
  • 8
1

try this code

$("#my-datepicker").datepicker().on('show.bs.modal', function(event) {
    // prevent datepicker from firing bootstrap modal "show.bs.modal"
    event.stopPropagation();
});
Reno Anthus
  • 714
  • 1
  • 9
  • 13
0

I tried replicating your issue but my form doesn't clear the other inputs when the datepicker is clicked.

View:

<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:

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

    });

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