Jquery datepicker更改事件触发器和输入的默认更改事件

问题描述:

我有datepicker

I have datepicker

  <input type='text' class='inp'>  
<script>
   $('.inp').datepicker();

  $(".inp").on("change",function (){ 
   console.log('inp changed');
  }
</script>

当我第一次更改.inp手动输入一个值,然后我点击datepicker的打开的日历我得到两个'更改'事件侦听器首先从手动更改,然后从datepicker更改
我如何避免这个?谢谢!

When I first change .inp manually type there a value, then immidiately I click on datepicker's opened calendar. I get two 'change' event listeners. First from manual change, then from datepicker change. How can I avoid this? Thanks!

设置您的输入 readOnly ,它将帮助您仅通过图标更改字段的值。

Set your input readOnly, it will help you to change the value of field through icon only.

<input type='text' class='inp' readOnly />

然后使用 onSelect 来获取选定的日期,如下所示:

then use onSelect to get selected date, as following:

$(function() {
    $(".inp").datepicker({
        onSelect: function(dateText, inst) {
            // alert(dateText);
        }
    });
});