将日期选择器动态添加到文本框中
Dynamically adding datepicker into a textbox
当我的页面加载时,我有一行,通过单击添加按钮,它将再添加一行。但从下面的代码来看,日期选择器只显示第一行,而不是动态添加行。如果我删除"第2部分"代码,则反向操作将起作用。我两者都需要。请帮忙。
//===========portion 1============
var i = 0;
$(document).ready(function() {
$("#add").click(function() {
i++;
$(".normal-tble tbody tr:first").clone().find("input,img").each(function() {
var tempId = $(this).attr("id");
var name_Attr = $(this).attr("name");
$(this).attr("id",$(this).attr("id")+"_"+(i))
if(tempId!='availability' && tempId!='remove') {
$(this).attr("name",name_Attr.replace('0',i));
if(tempId=='number') {
$(this).on("blur",isValidNumber);
$(this).val(0);
} else {
$(this).val('');
}
if(tempId=='expectedServiceDate') {
$(this).datepicker({changeMonth: true,changeYear: true,showButtonPanel: true,dateFormat: 'yy-mm-dd',minDate:0});
}
} else if(tempId=='availability') {
$(this).on("click",checkAvaialability);
} else if(tempId=='remove') {
$(this).on("click",onRemove);
}
}).end().appendTo(".normal-tble");
});
//=======portion 2===============
$('#expectedServiceDate').datepicker({changeMonth: true,changeYear: true,showButtonPanel: true,dateFormat: 'yy-mm-dd',minDate:0});
//==============================================================
<table class="normal-tble">
<thead>
<tr>
<th scope="col">Number</th>
<th scope="col">For Service</th>
<th scope="col">Expected Service Date</th>
<th scope="col">Comments</th>
<th scope="col">Check Availability</th>
<th scope="col">Remove</th>
<th scope="col"><img src="<c:url value="/resources/images/add2.png"/>" width="18" height="17" id="add"></th>
</tr>
</thead>
<tbody>
<tr class="second">
<td style="text-align:center"><form:input path="premiumList[0].number" id="number" class="tfl-02 qtyText"/></td>
<td style="text-align:center"><form:input path="premiumList[0].forService" id="forService" class="tfl-02"/></td>
<td style="text-align:center"><form:input path="premiumList[0].expectedServiceDate" id="expectedServiceDate" class="tfl-02" readonly="true"/></td>
<td style="text-align:center"><form:input path="premiumList[0].comments" id="comments" class="tfl-02"/></td>
<td style="text-align:center"><img src="<c:url value="/resources/images/view.png"/>" alt="view" id="availability"></td>
<td style="text-align:center"><img src="<c:url value="/resources/images/remove.png"/>" alt="remove" id ="remove"></td>
<td></td>
</tr>
</tbody>
</table>
这是因为元素尚未创建。为了对动态行进行事件处理,您需要以下内容:
$("body").on("focus", ".mydate", function () {
$(this).datepicker({
todayBtn: "linked"
})
});
相关文章:
- Javascript,将文本添加到具有现有文本节点的元素中
- 选中单选框时,将文本添加到文本框中
- 为未标记的文本添加CSS样式
- 如何将表格中的文本添加到使用按钮输入的文本区域
- 有没有办法将mouseOver上的个人文本添加到完整日历中
- 改变“;onClick"这个jQuery的操作从清除输入文本改为将输入文本添加到下面的列表中
- 如何使用JavaScript将链接文本添加到电子邮件中
- 将动态验证文本添加到自定义jQuery验证规则中
- 将文本添加到每个数组元素的开头
- 单击将按钮中的文本添加到带有jQuery或JS的输入框中
- 通过按钮将不同的文本添加到文本区域-不起作用
- 如何为Fabric.js中的文本添加CSS属性
- 如何将预加载程序文本添加到jQuery Cycle 2中
- Javascript Regex exec 在向搜索文本添加属性/引号后冻结
- 如何在所有浏览器中向文本区域中的选定文本添加 标记
- jQuery将文本添加到放置在表格单元格中的图像中
- D3 垂直条形图为标签文本添加换行符
- 单击时从列表中将文本添加到文本框的方法
- 将文本添加到 D3 圆环图的中心
- 如何将非可变文本添加到javascript打印弹出窗口中