【问题标题】:Hiding jquery datepicker when clicking on button单击按钮时隐藏jquery datepicker
【发布时间】:2017-11-22 20:59:17
【问题描述】:

我有一个 jquery 日期选择器,我遇到的问题是我希望能够在单击按钮/文本字段时切换日期选择器以打开和关闭,但现在我必须选择一天或单击外部日历(屏幕上的某处)关闭日期选择器。我想通过在同一个按钮上再次按下(切换)来打开和关闭它。

仅供参考 - 我知道 datepicker 有一个 hide 方法,但不确定这是否可以在这里使用

这里是Fiddle 的链接和下面的一些代码

$('#date1').datepicker({
    changeMonth: true,
    changeYear: true,
    showButtonPanel: true,
    dateFormat: "m/d/yy"
});
#ui-datepicker-div { font-size: 12px; } 
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
Date Picker on input field: <input type="text" id="date1" name="date1"/> <br/>

【问题讨论】:

  • “我想通过在同一个按钮上再次按下(切换)来打开和关闭它。”什么按钮?
  • 也许你想要的是icon trigger
  • 不,我想显示一个带有日期的文本字段,我希望这是他们单击以显示/隐藏日期选择器的字段

标签: javascript jquery html datepicker


【解决方案1】:

试试:

$('#date1').click(function(e) {
  if ($(this).data('count')) { // toggle
    if ($('#ui-datepicker-div').is(':visible')){
         $('#date1').datepicker('hide');
    } else {
            $('#date1').datepicker('show');
    }
   } else { // first click
     $(this).data('count', 1);
  }
});
$('#date1').blur(function() {
  $(this).data('count', '')
});

 $('#date1').datepicker({
    changeMonth: true,
    changeYear: true,
    showButtonPanel: true,
    dateFormat: "m/d/yy"
});
#ui-datepicker-div { 
    font-size: 12px; 
    position: relative !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

Date Picker on input field: <input type="text" id="date1" name="date1"/> <br/>

【讨论】:

  • 这个就是他要找的那个。我的答案不是解决方案
  • 我试过这个,当你第一次点击按钮时,它会打开然后立即关闭。它不会保持打开状态。
  • @user1186050 请立即查看.. :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-09
  • 2016-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多