admin管理员组文章数量:1430551
I have a Bootstrap modal where you can select a date. In the background, hidden fields are populated that are also submitted with the form.
The issue is that when you select the datepicker
element, it removes the hidden value for some strange reason (but only the hidden values populated by the Javascript).
Datepicker JS:
var date = new Date();
date.setDate(date.getDate());
$('#datepicker').datepicker({
format: "dd/mm/yyyy",
startDate: date,
autoclose: true,
});
Hidden field Populating JS:
$(function () {
$('#appointment').on("show.bs.modal", function (e) {
$("#request_id").val($(e.relatedTarget).data('request-id'));
});
});
Hidden HTML element:
<input type="hidden" name="request_id" id="request_id" value="">
When the Modal box initially pops up, I can see the hidden value field is populated, but when I click the datepicker
, it's removed. Why is this?
I have a Bootstrap modal where you can select a date. In the background, hidden fields are populated that are also submitted with the form.
The issue is that when you select the datepicker
element, it removes the hidden value for some strange reason (but only the hidden values populated by the Javascript).
Datepicker JS:
var date = new Date();
date.setDate(date.getDate());
$('#datepicker').datepicker({
format: "dd/mm/yyyy",
startDate: date,
autoclose: true,
});
Hidden field Populating JS:
$(function () {
$('#appointment').on("show.bs.modal", function (e) {
$("#request_id").val($(e.relatedTarget).data('request-id'));
});
});
Hidden HTML element:
<input type="hidden" name="request_id" id="request_id" value="">
When the Modal box initially pops up, I can see the hidden value field is populated, but when I click the datepicker
, it's removed. Why is this?
- grab and store form values with .val(); – Lieutenant Dan Commented Mar 3, 2017 at 12:41
2 Answers
Reset to default 8 +50The issue is that show.bs.modal
is fired when bootstrap datepicker is opened. So you simply have to set #request_id
value only when the show.bs.modal
is related to modal opening. One way you can use to get the element that fired the event is checking e.target.id
value inside your handler.
Here a working sample:
$('#datepicker').datepicker({
format: "dd/mm/yyyy",
startDate: new Date(),
autoclose: true,
});
$(function () {
$('#appointment').on("show.bs.modal", function (e) {
if( e.target.id == 'appointment'){
$("#request_id").val($(e.relatedTarget).data('request-id'));
}
});
});
<link href="//cdnjs.cloudflare./ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<link href="//cdnjs.cloudflare./ajax/libs/bootstrap-datepicker/1.6.4/css/bootstrap-datepicker3.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare./ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare./ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare./ajax/libs/bootstrap-datepicker/1.6.4/js/bootstrap-datepicker.js"></script>
<input type="hidden" name="request_id" id="request_id" value="">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#appointment" data-request-id="myExampleId">
Open appointment modal
</button>
<div id="appointment" class='modal fade'>
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class='modal-header'>
<button type='button' class='close' data-dismiss='modal' aria-hidden='true'>×</button>
<h4 class="modal-title">Title</h4>
</div>
<div class="modal-body">
<div class="form-group">
<label for="datepicker">Date bootstrap</label>
<input type="text" id="datepicker">
</div>
</div>
</div>
</div>
</div>
Use trusted Bootstrap-datepicker. If you using the same and making $ajax call, prevent extra values while making $ajax() by whilte listing your form values.
本文标签: Javascript added HTML value is removed when datepicker is selectedStack Overflow
版权声明:本文标题:Javascript added HTML value is removed when datepicker is selected - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745558482a2663328.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论