admin管理员组文章数量:1331388
I have a bootstrap date picker which i want to alert when date is changed buit it is working something different
i am trying with simple jquery on.(change)
event so it is executing two times one when user clicks on datepicker icon and another when user selects the date
CODE
$('#deliveryDate').datepicker({
format: 'dd/mm/yyyy',
});
$("#deliveryDate").on("change", function(e) {
alert("hi")
});
<script src=".3.1/jquery.min.js"></script>
<script src=".1.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href=".1.2/css/bootstrap.min.css">
<link href="/[email protected]/css/gijgo.min.css" rel="stylesheet" type="text/css" />
<script src="/[email protected]/js/gijgo.min.js" type="text/javascript"></script>
<div class=" col-lg-3">
<label for="deliveryDate" id="monHeader"> Date:</label>
<input type="text" id="deliveryDate" name="deliveryDate" width="176" />
</div>
I have a bootstrap date picker which i want to alert when date is changed buit it is working something different
i am trying with simple jquery on.(change)
event so it is executing two times one when user clicks on datepicker icon and another when user selects the date
CODE
$('#deliveryDate').datepicker({
format: 'dd/mm/yyyy',
});
$("#deliveryDate").on("change", function(e) {
alert("hi")
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn./bootstrap/4.1.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn./bootstrap/4.1.2/css/bootstrap.min.css">
<link href="https://cdn.jsdelivr/npm/[email protected]/css/gijgo.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.jsdelivr/npm/[email protected]/js/gijgo.min.js" type="text/javascript"></script>
<div class=" col-lg-3">
<label for="deliveryDate" id="monHeader"> Date:</label>
<input type="text" id="deliveryDate" name="deliveryDate" width="176" />
</div>
I have tried
$("#deliveryDate").on("dp.change", function(e) {
alert('date');
});
but this one is not working
what i am trying to do is when user selects the date then alert should e not on click of datepicker
Share Improve this question asked Apr 2, 2019 at 10:57 manish thakurmanish thakur 82010 gold badges43 silver badges82 bronze badges 6- dp.change works for datetimepicker. stackoverflow./questions/6471959/… – Harshwardhan Sharma Commented Apr 2, 2019 at 11:03
- Possible duplicate of jQuery Datepicker onchange event issue – Refilon Commented Apr 2, 2019 at 11:03
- @Refilon that one is jquery date picker – manish thakur Commented Apr 2, 2019 at 11:07
- @HarshwardhanSharma i am using date-picker only so there is no need to talk about date-time picker – manish thakur Commented Apr 2, 2019 at 11:07
- Possible duplicate of Detect change to selected date with bootstrap-datepicker – reisdev Commented Apr 2, 2019 at 11:10
2 Answers
Reset to default 4You can pare the values, and when there are chagnes, trigger the alert
$("#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./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn./bootstrap/4.1.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn./bootstrap/4.1.2/css/bootstrap.min.css">
<link href="https://cdn.jsdelivr/npm/[email protected]/css/gijgo.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.jsdelivr/npm/[email protected]/js/gijgo.min.js" type="text/javascript"></script>
<div class=" col-lg-3">
<label for="deliveryDate" id="monHeader"> Date:</label>
<input type="text" id="deliveryDate" name="deliveryDate" width="176" />
</div>
all you need to replace "change" to "focusout": hope this helps you:
$('#deliveryDate').datepicker({
format: 'dd/mm/yyyy',
});
$("#deliveryDate").on("focusout", function(e) {
e.preventDefault();
alert("hi")
});
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn./bootstrap/4.1.2/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn./bootstrap/4.1.2/css/bootstrap.min.css">
<link href="https://cdn.jsdelivr/npm/[email protected]/css/gijgo.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdn.jsdelivr/npm/[email protected]/js/gijgo.min.js" type="text/javascript"></script>
<div class=" col-lg-3">
<label for="deliveryDate" id="monHeader"> Date:</label>
<input type="text" id="deliveryDate" name="deliveryDate" width="176" />
</div>
本文标签: javascriptbootstrap datepicker on changeStack Overflow
版权声明:本文标题:javascript - bootstrap datepicker on change - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742282453a2446344.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论