我在动态行表中使用日期选择器存在问题.
>如果输入字段的ID插入了索引(例如:price_date [0]),则可以使用.
>如果没有索引(例如:price_date []),则会创建错误.无法设置属性currentDay的未定义.
>显示日期选择器,但单击日期时将显示错误.
>仅第一个price_date []未显示错误.
i = parseInt($('#counter').val());
$("#add_row").click(function(){
$('#addr'+i).html("<td align='right'>"+ (i+1) +"</td>\
<td class='col-xs-2'>\
<input type='text' name='harga_start_date[]' id='harga_start_date[]' class='datepick form-control' >\
</td>");
$('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
i++;
});
$("#delete_row").click(function(){
if(i> 1){
$("#addr"+(i-1)).html('');
i--;
}
});
$(document).on('focus',".datepick", function(){
$(this).datepicker({
dateFormat : 'yy-mm-dd',
changeMonth: true,
changeYear: true
});
});
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<table class="table table-striped" id="tab_logic">
<thead>
<tr>
<td colspan="7">
<a id="add_row" class="btn btn-success btn-sm pull-left">Add Row</a><a id="delete_row" class="pull-right btn btn-danger btn-sm">Delete Row</a>
</td>
</tr>
<tr class="success">
<th class="text-center">No.</th>
<th class="text-center">Start Date</th>
</tr>
</thead>
<tbody >
<tr id='addr0' class="itemsGroup">
<td align="right">1
</td>
<input type="hidden" name="counter" id="counter" value=1>
<td class="col-xs-8">
<input type="text" name="harga_start_date[]" id="harga_start_date[]" class="datepick form-control" >
</td>
</tr>
<tr id='addr1'></tr>
</table>
这是小提琴:https://jsfiddle.net/d4csw1kx/
解决方法:
如果我正确理解问题,则必须更改2件事.
>删除ids属性(您已经知道这一点).
>这里的一个大问题是,您可以在任何焦点上初始化插件,而插件本身会负责.
因此,解决方案是分别初始化任何输入(第一个和第二个输入)(加载页面时以及每次添加一行后).
i = parseInt($('#counter').val());
$("#add_row").click(function () {
var addr = $('#addr' + i).html("<td align='right'>" + (i + 1) + "</td>\
<td class='col-xs-2'>\
<input type='text' name='harga_start_date[]' class='datepick form-control' >\
</td>");
$('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
attachDatepicker(addr.find('input'));
i++;
});
$("#delete_row").click(function () {
if (i > 1) {
$("#addr" + (i - 1)).html('');
i--;
}
});
function attachDatepicker(input) {
input.datepicker({
dateFormat: 'yy-mm-dd',
changeMonth: true,
changeYear: true
});
}
attachDatepicker($('input'));
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.css" />
<table class="table table-striped" id="tab_logic">
<thead>
<tr>
<td colspan="7">
<a id="add_row" class="btn btn-success btn-sm pull-left">Add Row</a><a id="delete_row" class="pull-right btn btn-danger btn-sm">Delete Row</a>
</td>
</tr>
<tr class="success">
<th class="text-center">No.</th>
<th class="text-center">Start Date</th>
</tr>
</thead>
<tbody >
<tr id='addr0' class="itemsGroup">
<td align="right">1
</td>
<input type="hidden" name="counter" id="counter" value=1>
<td class="col-xs-8">
<input type="text" name="harga_start_date[]" id="harga_start_date[]" class="datepick form-control" >
</td>
</tr>
<tr id='addr1'></tr>
</table>
http://jsbin.com/yujeda/edit?html,js