gpt4 book ai didi

jquery - 带有输入的动态创建行上的日期选择器

转载 作者:行者123 更新时间:2023-12-03 22:20:52 25 4
gpt4 key购买 nike

我有一个表单,可以使用输入动态创建新行,每个新行上的日期输入应该有一个日期选择器。我几乎可以正常工作,但是当创建带有输入的新行时,日期选择器将不再在已经存在的日期字段上工作。我玩了一整天来找出我做错了什么,但我就是不知道如何解决这个问题。

这是 fiddle -> http://jsfiddle.net/HermesTrismegistus/vdFaH

我的表单如下所示:

<table id="productTable" class="table table-striped table-condensed">
<thead>
<tr>
<th>Product</th>
<th>Datum</th>
<th>Tijd</th>
<th>Minuten</th>
</tr>
</thead>
<tbody>
<tr>
<td><input id="products" class="input-medium" name="products[]" type="text" /></td>
<td><input id="date" class="datepick input-mini" name="date[]" type="text" /></td>
<td><input id="time" class="input-mini" name="time[]" type="text" /></td>
<td><input id="minutes" class="input-mini" name="minutes[]" type="text" /></td>
<td><a id="addnew" href=""><i class="icon-plus"></i></a></td>
</tr>
</tbody>
</table>

我拥有的jquery,看起来像这样:

    $(function(){
// start a counter for new row IDs
// by setting it to the number
// of existing rows
$('.datepick').datepicker();

var newRowNum = 0;

// bind a click event to the "Add" link
$('#addnew').click(function(){
// increment the counter
newRowNum = $(productTable).children('tbody').children('tr').length +1;

// get the entire "Add" row --
// "this" refers to the clicked element
// and "parent" moves the selection up
// to the parent node in the DOM
var addRow = $(this).parent().parent();

// copy the entire row from the DOM
// with "clone"
var newRow = addRow.clone();

// set the values of the inputs
// in the "Add" row to empty strings
$('input', addRow).val('');

// insert a remove link in the last cell
$('td:last-child', newRow).html('<a href="" class="remove"><i class="icon-minus"><\/i><\/a>');

// insert the new row into the table
// "before" the Add row
addRow.before(newRow);

// add the remove function to the new row
$('a.remove', newRow).click(function(){
$(this).closest('tr').remove();
return false;
});

$('#date', newRow).each(function(i){
var newID = 'date_' + i;
$(this).attr('id',newID);
});

// prevent the default click
return false;
});

最佳答案

尝试一下,当您添加一行时,销毁所有日期选择器实例,然后在追加新行后重新绑定(bind)日期选择器。

<强> jsFiddle example

关于jquery - 带有输入的动态创建行上的日期选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12093030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com