Bootstrap 日期时间选择器无法使用动态字段
Bootstrap Datetimepicker not working with dynamic field
我的当前代码是 bootstrap 日期时间选择器 Bootstrap 3 Datepicker,我目前遇到的问题是当我点击 "Add more fields" 新的 div 时带有 datetimerpicker 的文本字段不起作用,有什么想法吗?
<script type="text/javascript">
$(document).ready(function() {
var max_fields = 5; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div>\
<div class="input-group date datepicker_init">\
<input class="form-control" type="text" name="mytext[]">\
<span class="input-group-addon">\
<span class="glyphicon glyphicon-calendar"></span>\
</span>\
</div>\
<div class="input-group date datepicker_end">\
<input class="form-control" type="text" name="mytext[]">\
<span class="input-group-addon">\
<span class="glyphicon glyphicon-calendar"></span>\
</span>\
</div>\
<a href="#" class="remove_field red-color">Remove</a>\
<div class="voffset10"></div>\
</div>\
');
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
$('.datepicker_init').datetimepicker({
locale: 'es',
format: 'YYYY-MM-DD HH:mm'
});
$('.datepicker_end').datetimepicker({
locale: 'es',
format: 'YYYY-MM-DD HH:mm',
useCurrent: false
});
$(".datepicker_init").on("dp.change", function (e) {
$('.datepicker_end').data("DateTimePicker").minDate(e.date);
});
$(".datepicker_end").on("dp.change", function (e) {
$('.datepicker_init').data("DateTimePicker").maxDate(e.date);
});
});
</script>
<div class="form-group">
<div class="input_fields_wrap">
<button class="add_field_button btn btn-info btn-block voffset10">Add More Fields</button>
<div>
<div class='input-group date datepicker_init'>
<input class="form-control" type="text" name="mytext[]">
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<div class='input-group date datepicker_end'>
<input class="form-control" type="text" name="mytext[]">
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<div class="voffset10">
</div>
</div>
更新部分
$(".datepicker_init").on("dp.change", function (e) {
$('.datepicker_end').data("DateTimePicker").minDate(e.date);
});
$(".datepicker_end").on("dp.change", function (e) {
$('.datepicker_init').data("DateTimePicker").maxDate(e.date);
});
在添加新 html 后立即在点击处理程序中调用日期选择器初始化脚本,如下所示:
$(add_button).click(function(e){
// on add input button click
e.preventDefault();
// max input box allowed
// Reduce nesting!
if(x >= max_fields){
return
}
//text box increment
x++;
var tmp = $(wrapper).append('<div>\
<div class="input-group date datepicker_init">\
<input class="form-control" type="text" name="mytext[]">\
<span class="input-group-addon">\
<span class="glyphicon glyphicon-calendar"></span>\
</span>\
</div>\
<div class="input-group date datepicker_end">\
<input class="form-control" type="text" name="mytext[]">\
<span class="input-group-addon">\
<span class="glyphicon glyphicon-calendar"></span>\
</span>\
</div>\
<a href="#" class="remove_field red-color">Remove</a>\
<div class="voffset10"></div>\
</div>');
// Init the new pickers
$('.datepicker_init', tmp).datetimepicker({
locale: 'es',
format: 'YYYY-MM-DD HH:mm'
});
$('.datepicker_end', tmp).datetimepicker({
locale: 'es',
format: 'YYYY-MM-DD HH:mm',
useCurrent: false
});
});
首先,您的日期时间选择器绑定发生在 div 时 $(document).ready 不在动态 div 创建时间,因为 add_button 点击事件创建您的动态 div 这次没有绑定 datetimepicker。因此,在创建动态 div 之后,您必须同时为此 div 绑定 datetimepicker。下面的改动可以帮助你理解-
将您的以下代码放在任何函数中并在两个时间调用此函数,即在 document.ready 时间和 div 创建时间
function yourfunction(){
$('.datepicker_init').datetimepicker({
locale: 'es',
format: 'YYYY-MM-DD HH:mm'
});
$('.datepicker_end').datetimepicker({
locale: 'es',
format: 'YYYY-MM-DD HH:mm',
useCurrent: false
});
}
最后你的脚本看起来像这样
$(document).ready(function() {
var max_fields = 5; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div>\
<div class="input-group date datepicker_init">\
<input class="form-control" type="text" name="mytext[]">\
<span class="input-group-addon">\
<span class="glyphicon glyphicon-calendar"></span>\
</span>\
</div>\
<div class="input-group date datepicker_end">\
<input class="form-control" type="text" name="mytext[]">\
<span class="input-group-addon">\
<span class="glyphicon glyphicon-calendar"></span>\
</span>\
</div>\
<a href="#" class="remove_field red-color">Remove</a>\
<div class="voffset10"></div>\
</div>\
');
}
yourfunction();
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
yourfunction();
$(".datepicker_init").on("dp.change", function (e) {
$('.datepicker_end').data("DateTimePicker").minDate(e.date);
});
$(".datepicker_end").on("dp.change", function (e) {
$('.datepicker_init').data("DateTimePicker").maxDate(e.date);
});
});
我的当前代码是 bootstrap 日期时间选择器 Bootstrap 3 Datepicker,我目前遇到的问题是当我点击 "Add more fields" 新的 div 时带有 datetimerpicker 的文本字段不起作用,有什么想法吗?
<script type="text/javascript">
$(document).ready(function() {
var max_fields = 5; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div>\
<div class="input-group date datepicker_init">\
<input class="form-control" type="text" name="mytext[]">\
<span class="input-group-addon">\
<span class="glyphicon glyphicon-calendar"></span>\
</span>\
</div>\
<div class="input-group date datepicker_end">\
<input class="form-control" type="text" name="mytext[]">\
<span class="input-group-addon">\
<span class="glyphicon glyphicon-calendar"></span>\
</span>\
</div>\
<a href="#" class="remove_field red-color">Remove</a>\
<div class="voffset10"></div>\
</div>\
');
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
$('.datepicker_init').datetimepicker({
locale: 'es',
format: 'YYYY-MM-DD HH:mm'
});
$('.datepicker_end').datetimepicker({
locale: 'es',
format: 'YYYY-MM-DD HH:mm',
useCurrent: false
});
$(".datepicker_init").on("dp.change", function (e) {
$('.datepicker_end').data("DateTimePicker").minDate(e.date);
});
$(".datepicker_end").on("dp.change", function (e) {
$('.datepicker_init').data("DateTimePicker").maxDate(e.date);
});
});
</script>
<div class="form-group">
<div class="input_fields_wrap">
<button class="add_field_button btn btn-info btn-block voffset10">Add More Fields</button>
<div>
<div class='input-group date datepicker_init'>
<input class="form-control" type="text" name="mytext[]">
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<div class='input-group date datepicker_end'>
<input class="form-control" type="text" name="mytext[]">
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</div>
<div class="voffset10">
</div>
</div>
更新部分
$(".datepicker_init").on("dp.change", function (e) {
$('.datepicker_end').data("DateTimePicker").minDate(e.date);
});
$(".datepicker_end").on("dp.change", function (e) {
$('.datepicker_init').data("DateTimePicker").maxDate(e.date);
});
在添加新 html 后立即在点击处理程序中调用日期选择器初始化脚本,如下所示:
$(add_button).click(function(e){
// on add input button click
e.preventDefault();
// max input box allowed
// Reduce nesting!
if(x >= max_fields){
return
}
//text box increment
x++;
var tmp = $(wrapper).append('<div>\
<div class="input-group date datepicker_init">\
<input class="form-control" type="text" name="mytext[]">\
<span class="input-group-addon">\
<span class="glyphicon glyphicon-calendar"></span>\
</span>\
</div>\
<div class="input-group date datepicker_end">\
<input class="form-control" type="text" name="mytext[]">\
<span class="input-group-addon">\
<span class="glyphicon glyphicon-calendar"></span>\
</span>\
</div>\
<a href="#" class="remove_field red-color">Remove</a>\
<div class="voffset10"></div>\
</div>');
// Init the new pickers
$('.datepicker_init', tmp).datetimepicker({
locale: 'es',
format: 'YYYY-MM-DD HH:mm'
});
$('.datepicker_end', tmp).datetimepicker({
locale: 'es',
format: 'YYYY-MM-DD HH:mm',
useCurrent: false
});
});
首先,您的日期时间选择器绑定发生在 div 时 $(document).ready 不在动态 div 创建时间,因为 add_button 点击事件创建您的动态 div 这次没有绑定 datetimepicker。因此,在创建动态 div 之后,您必须同时为此 div 绑定 datetimepicker。下面的改动可以帮助你理解-
将您的以下代码放在任何函数中并在两个时间调用此函数,即在 document.ready 时间和 div 创建时间
function yourfunction(){
$('.datepicker_init').datetimepicker({
locale: 'es',
format: 'YYYY-MM-DD HH:mm'
});
$('.datepicker_end').datetimepicker({
locale: 'es',
format: 'YYYY-MM-DD HH:mm',
useCurrent: false
});
}
最后你的脚本看起来像这样
$(document).ready(function() {
var max_fields = 5; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div>\
<div class="input-group date datepicker_init">\
<input class="form-control" type="text" name="mytext[]">\
<span class="input-group-addon">\
<span class="glyphicon glyphicon-calendar"></span>\
</span>\
</div>\
<div class="input-group date datepicker_end">\
<input class="form-control" type="text" name="mytext[]">\
<span class="input-group-addon">\
<span class="glyphicon glyphicon-calendar"></span>\
</span>\
</div>\
<a href="#" class="remove_field red-color">Remove</a>\
<div class="voffset10"></div>\
</div>\
');
}
yourfunction();
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
yourfunction();
$(".datepicker_init").on("dp.change", function (e) {
$('.datepicker_end').data("DateTimePicker").minDate(e.date);
});
$(".datepicker_end").on("dp.change", function (e) {
$('.datepicker_init').data("DateTimePicker").maxDate(e.date);
});
});