我有一个引导日期选择器,我想在日期更改时提醒它,但它的工作方式有所不同
我正在尝试使用简单的jquery on.(change)
事件,因此它在用户单击datepicker图标时执行两次,而在用户选择日期时执行两次
代码
$('#deliveryDate').datepicker({
format: 'dd/mm/yyyy',
});
$("#deliveryDate").on("change", function(e) {
alert("hi")
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<link href="https://cdn.jsdelivr.net/npm/gijgo@1.9.6/css/gijgo.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.jsdelivr.net/npm/gijgo@1.9.6/js/gijgo.min.js" type="text/javascript"></script>
<div class=" col-lg-3">
<label for="deliveryDate" id="commonHeader"> Date:</label>
<input type="text" id="deliveryDate" name="deliveryDate" width="176" />
</div>
我尝试过
$("#deliveryDate").on("dp.change", function(e) {
alert('date');
});
但是这个不起作用
我想做的是当用户选择日期时,警报应该不会出现在单击日期选择器上
答案 0 :(得分:2)
您可以比较这些值,并且当有杂物出现时,触发警报
$('#deliveryDate').datepicker({
format: 'dd/mm/yyyy',
});
var lastValue = null;
$("#deliveryDate").on("change", function(e) {
if(lastValue !== e.target.value){
alert("hi")
console.log(e.target.value)
lastValue = e.target.value;
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<link href="https://cdn.jsdelivr.net/npm/gijgo@1.9.6/css/gijgo.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.jsdelivr.net/npm/gijgo@1.9.6/js/gijgo.min.js" type="text/javascript"></script>
<div class=" col-lg-3">
<label for="deliveryDate" id="commonHeader"> Date:</label>
<input type="text" id="deliveryDate" name="deliveryDate" width="176" />
</div>
答案 1 :(得分:0)
您需要将“ change”更改为“ focusout”: 希望这对您有帮助:
$('#deliveryDate').datepicker({
format: 'dd/mm/yyyy',
});
$("#deliveryDate").on("focusout", function(e) {
e.preventDefault();
alert("hi")
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css">
<link href="https://cdn.jsdelivr.net/npm/gijgo@1.9.6/css/gijgo.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.jsdelivr.net/npm/gijgo@1.9.6/js/gijgo.min.js" type="text/javascript"></script>
<div class=" col-lg-3">
<label for="deliveryDate" id="commonHeader"> Date:</label>
<input type="text" id="deliveryDate" name="deliveryDate" width="176" />
</div>