gpt4 book ai didi

javascript - onchange函数被触发两次

转载 作者:行者123 更新时间:2023-12-01 08:29:43 26 4
gpt4 key购买 nike

我想将 JQuery 多日期选择器中的值用于不同的目的,但由于某种原因,当我对得到的值进行测试时,我不断得到相同的东西两次。我错过了什么吗?

我的代码:

$('.date').datepicker({
multidate: true,
format: 'dd-mm-yyyy'
});

function printDates() {
var dates = document.getElementById("date_input").value;
console.log(dates);

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js'></script>


<input type="text" name="dates" class="form-control date" id="date_input" placeholder="Pick the dates " onchange="printDates()">

最佳答案

使用changeDate代替onchange,如下所示

$('.date').datepicker({
multidate: true,
format: 'dd-mm-yyyy'
})
.on('changeDate', function(e) {
printDates(); // `e` here contains the extra attributes
});
;

function printDates() {
var dates = document.getElementById("date_input").value;
console.log(dates);

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js'></script>


<input type="text" name="dates" class="form-control date" id="date_input" placeholder="Pick the dates " >

关于javascript - onchange函数被触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61819607/

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