Javascript 在 jQuery 日期选择器上收听 onChange?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30132747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Listening to onChange on a jQuery datepicker?
提问by John Pizzo
I currently am creating a date picker like so
我目前正在创建一个像这样的日期选择器
HTML
HTML
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<input id="datepicker" type="datepicker" name="date" placeholder="Date" />
JS
JS
$('#datepicker').datepicker()
.on("change", function (e) {
console.log("Date changed: ", e.target.value);
});
My goal is to be able to listen for when the user clears out the date box. However, the change event only seems to fire once you unfocus the date box. I'm wondering if there's a way to listen for all changes in a jQuery date box, or if there are any better alternatives.
我的目标是能够监听用户何时清除日期框。但是,更改事件似乎只有在您取消关注日期框后才会触发。我想知道是否有办法侦听 jQuery 日期框中的所有更改,或者是否有更好的选择。
JsFiddle link: https://jsfiddle.net/szkfm0y7/
JsFiddle 链接:https://jsfiddle.net/szkfm0y7/
回答by Robin Carlo Catacutan
回答by Vimalan Jaya Ganesh
Here is a possible solution:https://jsfiddle.net/7atsua85/
这是一个可能的解决方案:https: //jsfiddle.net/7atsua85/
Instead of e.target.value, use $(this).val() as shown below,
使用 $(this).val() 代替 e.target.value,如下所示,
$('#datepicker').datepicker()
.on("change", function (e) {
//console.log("Date changed: ", e.target.value);
alert($(this).val())
});

