【问题标题】:When i add new datepicker dynamically the previous datepicker values reset?当我动态添加新的日期选择器时,以前的日期选择器值会重置吗?
【发布时间】:2017-04-03 11:12:50
【问题描述】:

我的表单包含添加更多字段按钮,每当我点击添加更多字段按钮时,它都会动态添加新的datepicker

问题是,datepicker 中的当前日期字段显示当前日期。 但是,当我通过单击 Add more Field Button 添加新的 Datepicker 时,它会再添加一个 Datepicker 但这一次它会重置之前的值Datepicker 我手动设置的。

以下是我的代码: html

<div id="container">
<input class="datepick" type="text"></input><button class="add">Add</button>
<input 
</div>

javascript

 $(".datepick").datepicker();
$(".datepick").datepicker().datepicker("setDate", new Date());
$(document).on('click', '.add', function() {
    var appendTxt = '<input class="datepick"  type="text" /><button class="add">Add</button>';
    $("#container").append(appendTxt);
    $(".datepick").datepicker();
    $(".datepick`enter code here`").datepicker().datepicker("setDate", new Date());

});

【问题讨论】:

  • 你调试过代码流吗?两个日期选择器不能共享相同的 id?

标签: javascript jquery html jquery-ui datepicker


【解决方案1】:

因为这条线:

$(".datepick").datepicker();

所有具有.datepick 类的输入都将重新初始化。你应该这样做:

$(".datepick").last().datepicker();

$(".datepick:last").datepicker();

添加新的input.datepick 之后。这是简单的解决方案。

如果您在此容器之后的另一个容器中还有其他 .datepick,则此方法也不起作用。您应该非常具体地了解附加的 datepicker 元素。

你应该这样做:

var appendTxt = '<input class="datepick"  type="text" /><button class="add">Add</button>';
var newInput = $(appendTxt).appendTo("#container");
newInput.datepicker();
newInput.datepicker().datepicker("setDate", new Date());

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    相关资源
    最近更新 更多