如何动态创建bootstrap datepicker?
How to dynamically create bootstrap datepicker?
我需要使用 jQuery 动态创建日期选择器。在我的表单中,我将有按钮来创建日期选择器。
下面是我到目前为止尝试过的代码:
<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>
而JavaScript代码如下:
$("#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
});
基本上我最初只显示一个日期选择器。然后我有一个按钮来添加更多日期选择器。 datepicker 的模板是隐藏的,点击 "add more" 按钮我只是克隆它并将它添加到 DOM.
使用下面的代码
$('body').on('focus',".examDate", function(){
$(this).datetimepicker({
format: 'DD/MM/YYYY',
ignoreReadonly: true,
showTodayButton: true
});
});
这样动态创建的元素也可以初始化日期时间选择器。
已发布工作示例:Jsfiddle
$('body').on('DOMNodeInserted', function (e) {
if (e.target.classList.contains('datepicker-container')) {
$('.datepicker').datepicker({
format: 'DD/MM/YYYY',
ignoreReadonly: true,
showTodayButton: true
});
}
})
每次将日期选择器容器(显然包含日期选择器)添加到 DOM 时,都会初始化日期选择器。这就是我解决这个问题的方法,希望这对以后有所帮助。
更新
DOMInsertedNode 的使用现已弃用一段时间。相反,您可以根据以下内容尝试一些操作,使用 MutationObservers.
new MutationObserver(function (mutations) {
var selector = ".datepicker";
// look through all mutations that just occured
for (var i = 0; i < mutations.length; ++i) {
// look through all added nodes of this mutation
for (var j = 0; j < mutations[i].addedNodes.length; ++j) {
var target = mutations[i].addedNodes[j];
if (target && target.nodeType === Node.ELEMENT_NODE) {
var children = $(target).find(selector);
if (target.matches(selector)) {
$(target).datepicker({
format: 'DD/MM/YYYY',
ignoreReadonly: true,
showTodayButton: true
});
}
else if (children.length > 0) {
children.each(function () {
$(this).datepicker({
format: 'DD/MM/YYYY',
ignoreReadonly: true,
showTodayButton: true
});
});
}
}
}
}
}).observe(document.querySelector("body"), {
childList: true,
subtree: true
});
我需要使用 jQuery 动态创建日期选择器。在我的表单中,我将有按钮来创建日期选择器。
下面是我到目前为止尝试过的代码:
<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>
而JavaScript代码如下:
$("#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
});
基本上我最初只显示一个日期选择器。然后我有一个按钮来添加更多日期选择器。 datepicker 的模板是隐藏的,点击 "add more" 按钮我只是克隆它并将它添加到 DOM.
使用下面的代码
$('body').on('focus',".examDate", function(){
$(this).datetimepicker({
format: 'DD/MM/YYYY',
ignoreReadonly: true,
showTodayButton: true
});
});
这样动态创建的元素也可以初始化日期时间选择器。
已发布工作示例:Jsfiddle
$('body').on('DOMNodeInserted', function (e) {
if (e.target.classList.contains('datepicker-container')) {
$('.datepicker').datepicker({
format: 'DD/MM/YYYY',
ignoreReadonly: true,
showTodayButton: true
});
}
})
每次将日期选择器容器(显然包含日期选择器)添加到 DOM 时,都会初始化日期选择器。这就是我解决这个问题的方法,希望这对以后有所帮助。
更新
DOMInsertedNode 的使用现已弃用一段时间。相反,您可以根据以下内容尝试一些操作,使用 MutationObservers.
new MutationObserver(function (mutations) {
var selector = ".datepicker";
// look through all mutations that just occured
for (var i = 0; i < mutations.length; ++i) {
// look through all added nodes of this mutation
for (var j = 0; j < mutations[i].addedNodes.length; ++j) {
var target = mutations[i].addedNodes[j];
if (target && target.nodeType === Node.ELEMENT_NODE) {
var children = $(target).find(selector);
if (target.matches(selector)) {
$(target).datepicker({
format: 'DD/MM/YYYY',
ignoreReadonly: true,
showTodayButton: true
});
}
else if (children.length > 0) {
children.each(function () {
$(this).datepicker({
format: 'DD/MM/YYYY',
ignoreReadonly: true,
showTodayButton: true
});
});
}
}
}
}
}).observe(document.querySelector("body"), {
childList: true,
subtree: true
});