jquery datepicker对附加元素不起作用

问题描述 投票:0回答:2

我试图将所有原始和附加元素作为日期选择器,但只有原始作品

我尝试了这段代码,但是下一行输入看起来像普通输入而不是jquery datepicker希望你能找到解决方案谢谢。

这是我的代码。

<html>
<head>
    <title></title>
</head>
<script type="text/javascript">


$(document).ready(function(){

    $(".inputdate").datepicker({


    });

    $(".samplebtn").click(function(){

        $(".samplediv").append("<input class='inputdate' type='text' name='date'>");

    });

});

</script>

<body>
    <div class="samplediv">
        <input class="inputdate" type="text" name="date">
    </div>
    <button class="samplebtn">Add</button>
</body>
</html>
javascript jquery datepicker
2个回答
1
投票

绑定动态项后重新初始化日期选择器。

$(".samplebtn").click(function() {
    $(".samplediv").append("<input class='inputdate' type='text' name='date'>");
    $('.samplediv').find(".inputdate").datepicker({

    });
}); 

0
投票

你应该将.datepicker()附加到那些新元素上。试试这个.append()脚本:

$(".samplediv")
.append("<input class='inputdate' type='text' name='date'>")
.promise()
.done(function() {
    $(this).find('.inputdate').datepicker({ ... });
});

希望这有帮助。 :d

© www.soinside.com 2019 - 2024. All rights reserved.