我在表单中有两个时间输入字段和两个日期字段。还有一个添加更多按钮来添加更多这样的行。时间输入插件从第二行开始不起作用。代码如下:
<link href="styles.css" rel="stylesheet" />
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript" src="http://www.jqueryscript.net/demo/jQuery-Plugin-for-Input-Field-Time-Format-Spinner-Time-Entry/jquery.timeentry.js"></script>
<link href="/css/jquery.timeentry.css" media="screen" rel="stylesheet" type="text/css" >
<script>
$(function () {
var newRow = $(".addrows").clone();
$(".datepicker").datepicker();
$('.time').timeEntry({show24Hours: true, spinnerImage: '', defaultTime : new Date()});
$("#addButton").on("click", function () {
newRow.clone().appendTo("#TextBoxesGroup tbody").find(".datepicker").datepicker();
});
});
</script>
<table id="TextBoxesGroup">
<tr class="addrows">
<td>Start Time:</td>
<td>
<input type="text" name="StartTime" id="StartTime" value="" class="time">
<button name="starttimenow" id="starttimenow" type="button" onclick="var currentDate = new Date(); var hours = currentDate.getHours(); var minutes = currentDate.getMinutes(); if(hours < 10) { hours = '0' + hours; } if(minutes < 10) { minutes = '0' + minutes; } $('#StartTime').val(hours + ':' + minutes);">Now</button></dd>
</td>
<td>Start Date:</td>
<td><input type="text" name="StartDate[]"class="datepicker" value="" autocomplete="off" size="6">
</td>
<td>End Time:</td>
<td>
<input type="text" name="EndTime" id="EndTime" value="" class="time">
<button name="endtimenow" id="endtimenow" type="button" onclick="var currentDate = new Date(); var hours = currentDate.getHours(); var minutes = currentDate.getMinutes(); if(hours < 10) { hours = '0' + hours; } if(minutes < 10) { minutes = '0' + minutes; } $('#EndTime').val(hours + ':' + minutes);">Now</button></dd>
</td>
<td>End Date:</td>
<td>
<input type="text" name="EndDate[]" class="datepicker" value="" autocomplete="off" size="6">
</td>
</tr>
</table>
<table>
<tr>
<td>
<input type="button" id="addButton" value="+" />
</td>
</tr>
</table>
我知道我可以改变身份。到了班级&#39;在行:
<input type="text" name="StartTime" id="StartTime" value="" class="time">
<input type="text" name="EndTime" id="EndTime" value="" class="time">
但是会有两个类,而且时间输入插件也无法正常工作。
请帮我解决这个问题。 谢谢!
答案 0 :(得分:1)
您的代码未检测新添加的行中的时间字段。我建议编写一个函数来检测容器元素(或元素)中的元素。您可以使用该函数在页面加载时检测现有行,并在添加新行时再次使用它。
您肯定需要避免重复的id
值。正如您在问题中提到的那样,您可以使用class
属性。
HTML:
<table id="TextBoxesGroup">
<tr>
<td>Start Time:</td>
<td>
<input type="text" name="StartTime[]" value="" class="time" />
<button type="button" class="nowBtn">Now</button>
</td>
<td>Start Date:</td>
<td>
<input type="text" name="StartDate[]" value="" class="datepicker" autocomplete="off" size="6" />
</td>
<td>End Time:</td>
<td>
<input type="text" name="EndTime[]" value="" class="time" />
<button type="button" class='nowBtn'>Now</button>
</td>
<td>End Date:</td>
<td>
<input type="text" name="EndDate[]" class="datepicker" value="" autocomplete="off" size="6" />
</td>
</tr>
</table>
<table>
<tr>
<td>
<input type="button" id="addButton" value="+" />
</td>
</tr>
</table>
JavaScript的:
$(function () {
// This function instruments elements inside the container element.
function instrument($containers) {
$containers.find('.time').timeEntry({
show24Hours: true,
spinnerImage: '',
defaultTime: new Date()
});
$containers.find('.datepicker').datepicker();
}
var $group = $('#TextBoxesGroup'),
$rows = $group.find('tr'),
$rowTemplate = $rows.first().clone();
// Instrument the existing rows.
instrument($rows);
// Use event delegation to handle the "Now" button clicks.
$group.on('click', '.nowBtn', function() {
var now = new Date(),
hours = ('0' + now.getHours()).slice(-2),
minutes = ('0' + now.getMinutes()).slice(-2);
$(this).parent().find('input').val(hours + ':' + minutes);
});
$('#addButton').click(function() {
var $row = $rowTemplate.clone();
$group.children('tbody').append($row);
// Instrument the newly added row.
instrument($row);
});
});
您还可以在instrument()
功能中为“现在”按钮注册点击处理程序,但我选择了如何使用事件委派来代替。