javascript - Bootstrap datepicker in popover clear other inputs when event show fired - Stack Overflow

I have a form in popover window, in this form I have input with datepicker bootstrap. I fill all inputs

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>

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>

Share Improve this question asked Jul 7, 2016 at 17:29 Pavel KononenkoPavel Kononenko 3303 silver badges16 bronze badges 1
  • Possible duplicate of stackoverflow./questions/23311222/… – Vivek Buddhadev Commented Jan 21, 2018 at 6:37
Add a ment  | 

3 Answers 3

Reset to default 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./uxsolutions/bootstrap-datepicker/issues/978

try this code

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

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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742344980a4426363.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信