我是jQuery的新手,现在已经有很长一段时间了解这个问题。
我在输入字段中使用jQuery ui datepicker,单击按钮时动态添加。日期选择器仅适用于输入框的第一个实例。尝试过寻找解决方案,但没有运气。任何帮助将不胜感激。感谢
jQuery代码
$( ".datepicker" ).datepicker({
inline: true,
dateFormat: "yy-mm-dd"
});
Html代码
<div id="newlink">
<div>
<table style="border: 1px solid black;">
<tr>
<th>Date:</td>
<td>
<input type="text" name="linkurl[]" id="Editbox18" class="datepicker" style="width: 147px; height: 23px; z-index: 2; color: black" autocomplete="off">
</td>
</tr>
<tr>
<th>Remark:</td>
</tr>
<tr>
<td>
<textarea name="linkdesc[]" size="1" id="Editbox19" style="width: 550px; height: 45px; z-index: 2; color: black" autocomplete="off"></textarea>
</td>
</tr>
</table>
<br>
</div>
</div>
<br>
<p id="addnew">
<input onclick="new_link()" type="button" name="linkdesc[]" value="Add More" style="width: 80px; height: 24px; z-index: 136; color: black;">
</p>
<!-- Template -->
<div id="newlinktpl" style="display: none"> <font size="1.5">
<hr style="width: 75%; margin-left: -5px;" /> <br />
<div>
<table style="border: 1px solid black;">
<th>Date:
</td>
<td><input type="text" name="linkurl[]" id="Editbox"
class="datepicker"
style="width: 147px; height: 23px; z-index: 2; color: black"
autocomplete="off"></td>
</tr>
<tr>
<th>Remark:
</td>
</tr>
<tr>
<td><textarea name="linkdesc[]" size="1" id="Editbox19"
style="width: 550px; height: 45px; z-index: 2; color: black"
autocomplete="off"></textarea></td>
</tr>
<script type="text/javascript">
var ct = 1;
function new_link() {
ct++;
var div1 = document.createElement('div');
div1.id = ct;
// link to delete extended form elements
var delLink = ' <input onclick="delIt(' + ct
+ ')" type="button" value="Del" style="position:relative; top:-20px; left:600px; color:black; width:50px;height:20px;z-index:136;">';
div1.innerHTML = document.getElementById('newlinktpl').innerHTML + delLink;
document.getElementById('newlink').appendChild(div1);
}
// function to delete the newly added set of elements
function delIt(eleId) {
d = document;
var ele = d.getElementById(eleId);
var parentEle = d.getElementById('newlink');
parentEle.removeChild(ele);
}
</script>
</td>
</tr>
</table>
</div>
</font>
</div>
答案 0 :(得分:1)
您需要在您创建的新输入元素中设置datePicker,但是如何复制具有所需日期选择器的输入,从新元素中删除类hasDatePicker。请尝试下面的代码。
代码已更新http://jsfiddle.net/L3X4D/1/
HTML
<div id="newlink">
<div>
<table style="border:1px solid black;">
<tr>
<th>Date:</td>
<td>
<input type="text" name="linkurl[]" id="Editbox18" class="datepicker" style="width:147px;height:23px;z-index:2; color:black" autocomplete="off">
</td>
</tr>
<tr>
<th>Remark:</td>
</tr>
<tr>
<td>
<textarea name="linkdesc[]" size="1" id="Editbox19" style="width:550px;height:45px;z-index:2; color:black" autocomplete="off"></textarea>
</td>
</tr>
</table>
<br>
</div>
</div>
<br>
<p id="addnew">
<input id="clickBTN" type="button" name="linkdesc[]" value="Add More" style="width:80px;height:24px;z-index:136; color:black;">
</p>
<!-- Template -->
<div id="newlinktpl" style="display:none"><font size="1.5">
<hr style="width:75%; margin-left:-5px;"/><br/>
<div>
<table style="border:1px solid black;">
<th>Date:</td>
<td> <input type="text" name="linkurl[]" id="Editbox" class="datepicker" style="width:147px;height:23px;z-index:2; color:black" autocomplete="off" >
</td></tr>
<tr>
<th>Remark:</td></tr><tr>
<td><textarea name="linkdesc[]" size="1" id="Editbox19" style="width:550px;height:45px;z-index:2; color:black" autocomplete="off" ></textarea>
</td></tr>
</td>
</tr>
</table>
</div></font>
</div>
JS
$(document).ready(function () {
$(".datepicker").datepicker({
inline: true,
dateFormat: "yy-mm-dd"
});
$("#clickBTN").click(function () {
new_link()
});
});
var ct = 1;
function new_link() {
ct++;
var div1 = $('<div></div>');
$(div1).attr('id', ct);
// link to delete extended form elements
var delLink = $('<input type="button" value="Del" style="position:relative; top:-20px; left:600px; color:black; width:50px;height:20px;z-index:136;">');
//set click to remove the element
$(delLink).click(function () {
$(div1).remove();
});
$(div1).append($('#newlinktpl').html());
$(div1).append(delLink);
// remove class hasDatePicker
$(div1).find('.datepicker').removeClass("hasDatepicker");
//renema input
$(div1).find('.datepicker').attr("ID","newInput"+ct);
//set the new input element how datepicker
$(div1).find('.datepicker').datepicker({
inline: true,
dateFormat: "yy-mm-dd"
});
$('#newlink').append(div1);
}
// function to delete the newly added set of elements
function delIt(eleId) {
d = document;
var ele = d.getElementById(eleId);
var parentEle = d.getElementById('newlink');
parentEle.removeChild(ele);
}
答案 1 :(得分:1)
据我记得,当使用多个日期选择器时,他们需要输入的唯一ID。因此,如果您要添加新内容,首先需要独特的ID。文档就绪是在加载所有文档元素时发生的单个事件。所以你需要绑定其他东西。 e.g。
HTML
<div id="linkscontainer">
<div id="newlink" class="linkbox">
<div style="border:1px solid black;"> <span>Date:</span>
<span><input type="text" name="newdate[]" value="" id="dates" class="date" /><span>
<div>Remark:</div>
<textarea name="linkdesc[]" size="1" id="Editbox19" style="width:550px;height:45px;z-index:2; color:black" autocomplete="off"></textarea>
</span></span></div>
<input type="button" class="delete" name="delete" id="delete" value="delete" style="width: 80px; height: 24px; z-index: 136; color: black;">
</div>
</div>
<input type="button" id="addnew" name="linkdesc[]" value="Add More" style="width: 80px; height: 24px; z-index: 136; color: black;">
jquery的
$(document).ready(function(){
var container= $('#newlink').html(); // container to add
var ids=1; //id numbers for add. datepickers
$('#addnew').click(function() {
$('#linkscontainer').append(container); // add container
$('.date:last').attr('id', ids);// each datepicker must have a unique id.
$('.delete:last').attr('id', 'f'+ids);// each deletebutton must have a unique id.
ids++; // increase the id numbers
});
});
$(document).on('focus',".date", function(){ //bind to all instances of class "date".
$(this).datepicker();
});
小提琴:
编辑:
删除按钮需要以相同的方式完成:
$(document).on('focus',".delete", function(){ //bind to all instances of class "date".
$(this).parent().remove();
});