$(function () {
$("#addButton").on("click", function () {
var newRow = $(".addrows").first().clone().addClass("newAdded"); newRow.appendTo("#TextBoxesGroup");
});
<form enctype="application/x-www-form-urlencoded" action="" method="post">
<table id="TextBoxesGroup" style="width:auto;margin:0 auto;" >
<tr class="addrows"><td class="required">
Start Time:<br/>
<input type="text" name="StartTime[]" id="StartTime" value="" class="time" size="5">
<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>
</td>
<td class='required' colspan="2">Start Date:<br/>
<input type="text" name="StartDate[]" id="StartDate" value="" class="date" autocomplete="off" size="6">
<button name="startdatetoday" id="startdatetoday" type="button" onclick="var currentDate = new Date(); var month=currentDate.getMonth()+1; if(month < 10) { month = '0' + month; } var day = currentDate.getDate(); if(day < 10) { day = '0' + day; } var year = currentDate.getFullYear(); $('#StartDate').val(day + '/' + month + '/' + year)">Today</button>
</td>
<td class='required' >End Time:<br/>
<input type="text" name="EndTime[]" id="EndTime" value="" class="time" size="5">
<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>
</td>
<td class='required' colspan="2" >End Date:<br/>
<input type="text" name="EndDate[]" id="EndDate" value="" class="date" autocomplete="off" size="6">
<button name="enddatetoday" id="enddatetoday" type="button" onclick="var currentDate = new Date(); var month=currentDate.getMonth()+1; if(month < 10) { month = '0' + month; } var day = currentDate.getDate(); if(day < 10) { day = '0' + day; } var year = currentDate.getFullYear(); $('#EndDate').val(day + '/' + month + '/' + year)">Today</button>
</td></tr>
</table>
<table>
<tr>
<td>
<input type="button" id="addButton" value="+" />
</td>
</tr>
<tr><td colspan="18" align="center">
<input type="submit" name="Submit" id="Submit" value="Submit" class="formsubmit">
</td></tr>
</table>
</form>
以上是我表格中的一个工作示例。当我按下+按钮时,我需要创建多个字段。新字段应为空。此外,当我按下/ now按钮时,我需要在第一个字段以外的确切字段中显示日期和时间。
请帮帮我。 谢谢!
答案 0 :(得分:1)
使用val('')
清除克隆的输入字段:
$("#addButton").on("click", function () {
var newRow = $(".addrows").first().clone().addClass("newAdded");
newRow.find(':input').val('');
newRow.appendTo("#TextBoxesGroup");
});
要引用正确的行字段,请使用类而不是id和prev
方法来查找正确的元素:
<input type="text" name="StartTime[]" class="StartTime" value="" class="time" size="5">
JS:
$(this).prev('.StartTime').val(hours + ':' + minutes);
答案 1 :(得分:1)
使用$(this).prev();
<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; } $(this).prev().val(hours + ':' + minutes);">Now</button>
答案 2 :(得分:0)
$("#addButton").on("click", function () {
var newRow = $(".addrows").first().clone().addClass("newAdded");
newRow.appendTo("#TextBoxesGroup");
$('.newAdded:last-child input').val('');
});