【问题标题】:bootstrap datepicker on change更改时引导日期选择器
【发布时间】:2019-08-23 16:17:09
【问题描述】:

我有一个引导日期选择器,我想在日期更改时提醒它,但它的工作方式不同

我正在尝试使用简单的 jquery on.(change) 事件,因此它在用户单击日期选择器图标时执行两次,在用户选择日期时执行另一次

代码

$('#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');
});

但是这个不工作

我想要做的是当用户选择日期时,不应该在点击日期选择器时发出警报

【问题讨论】:

标签: javascript jquery bootstrap-datepicker


【解决方案1】:

你可以比较值,当有变化时,触发警报

$("#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>

【讨论】:

    【解决方案2】:

    您只需将“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>

    【讨论】:

    • 非常感谢,Nemer,我已经尝试了几乎所有的方法,除了 focusout 没有任何效果
    猜你喜欢
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2017-12-23
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    相关资源
    最近更新 更多