问题
I need to create a date picker dynamically using jQuery. In my form I will have button to create date picker.
Below is the code I tried so far:
<div id="examDatesContainer">
<div class="form-group">
<label for="examDate" class="col-sm-2 caption">
<fmt:message key="exam.date"/>:<strong style="font-size:14px;" class="asterisk">*</strong>
</label>
<div class="col-sm-4">
<div class="input-group date">
<input type="datetime" class="form-control examDate" id="examDate" name="examDate" value="" readonly >
<div class="input-group-addon" class="examDateCalendarIcon">
<i class="fa fa-calendar"></i>
</div>
</div>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group examDateTemplate" style="display: none;">
<label for="examDate" class="col-sm-2 caption"></label>
<div class="col-sm-4">
<div class="input-group date">
<input type="datetime" class="form-control examDate" name="examDate" value="" readonly >
<div class="input-group-addon" class="examDateCalendarIcon">
<i class="fa fa-calendar"></i>
</div>
<div class="input-group-addon">
<i class="fa fa-minus-circle"></i>
</div>
</div>
</div>
</div>
</div>
<div class="form-group">
<label for="examDate" class="col-sm-2 caption">
</label>
<div class="col-sm-4">
<button id="addExamDateBtn" type="button" class="btn btn-sm btn-primary">
<span class="fa fa-plus"> </span>
<fmt:message key="add"/>
</button>
</div>
</div>
and the JavaScript code is as below:
$("#addExamDateBtn").click(function(){
var examDateClonedObj = $(".examDateTemplate").clone();
$(examDateClonedObj).removeClass("examDateTemplate");
$(examDateClonedObj).appendTo("#examDatesContainer");
$(examDateClonedObj).show();
$('.datepicker').datetimepicker('update');
});
$('.examDate').datetimepicker({
format: 'DD/MM/YYYY',
ignoreReadonly: true,
showTodayButton: true
});
Basically I show a single datepicker initially. Then I have a button to add more date pickers. Template for datepicker is hidden, on click of "add more" button I just clone it and add it to the DOM.
回答1:
Use this code below
$('body').on('focus',".examDate", function(){
$(this).datetimepicker({
format: 'DD/MM/YYYY',
ignoreReadonly: true,
showTodayButton: true
});
});
This way the dynamically created elements can also initialize the datetimepicker.
A working example is posted : Jsfiddle
回答2:
$('body').on('DOMNodeInserted', function (e) {
if (e.target.classList.contains('datepicker-container')) {
$('.datepicker').datepicker({
format: 'DD/MM/YYYY',
ignoreReadonly: true,
showTodayButton: true
});
}
})
Every time a datepicker-container (which obviously contains a datepicker) is added to the DOM, datepickers are initialized. This is how I solved this problem, hope this will help some in the future.
来源:https://stackoverflow.com/questions/42453230/how-to-dynamically-create-bootstrap-datepicker