【问题标题】:Auto Close is not working for date picker自动关闭不适用于日期选择器
【发布时间】:2017-07-11 14:27:29
【问题描述】:

我在日期选择器中有自定义按钮。按钮文本:完成。

要求:当我点击文本框时,日期选择器应该显示并选择一些日期。单击“完成”按钮后,所选日期应显示在文本框中,日期选择器应关闭。

我已将 autoclose 设置为 false

实际输出: 我在单击带有“完成”按钮的文本框时获取日期选择器,但是每当我选择任何日期时,日期选择器都会关闭。

我是做错了什么还是必须添加更多代码。

$(function () {
  $(".datepicker1").datepicker({
    showButtonPanel: true,
    autoclose: false,
    dateFormat: "yy-mm-dd",
    onSelect: function (dateText, inst) {
        alert('Date Selected');
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script type="text/javascript"  src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>    


<input class="datepicker1" type="text"  />

【问题讨论】:

  • 请尝试在sn-p中重现问题,datepicker库有很多

标签: javascript jquery html jquery-ui


【解决方案1】:

查看jQuery UI Datepicker API,似乎没有autoclose 选项。

利用this SO post 中的代码添加到onSelect 事件:

$().ready(function () {
    $(".datepicker1").datepicker({
      showButtonPanel: true,
      dateFormat: "yy-mm-dd",
      onSelect: function (dateText, inst) {
        alert('Date Selected');
        
        // keep dialog open
        if (inst.inline)
        	this._updateDatepicker(inst);
        else {
          this._hideDatepicker(null, this._get(inst, 'duration'));
          this._lastInput = inst.input[0];
          if (typeof(inst.input[0]) != 'object')
            inst.input[0].focus(); // restore focus
          this._lastInput = null;
        }
      }});
    });
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

<input class=datepicker1 type=text>

【讨论】:

  • 万岁,你和我的答案都是一样的。非常感谢你。
【解决方案2】:

我得到了答案。

我在 OnSelect 事件中添加了以下代码

 if (inst.inline) this._updateDatepicker(inst);
                else {
                    this._hideDatepicker(null, this._get(inst, 'duration'));
                    this._lastInput = inst.input[0];
                    if (typeof (inst.input[0]) != 'object') inst.input[0].focus(); // restore focus
                    this._lastInput = null;
                }

【讨论】:

    猜你喜欢
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多