【问题标题】:Open Datepicker by clicking a Link通过单击链接打开 Datepicker
【发布时间】:2013-04-10 20:56:04
【问题描述】:

我在我的网页上使用 JQuery UI 的 Datepicker。目前,我已经成功地让它与一个名为“mydate”的文本字段一起工作。代码如下:

$('#releasedate').datepicker({
                changeYear: 'true',
                changeMonth:'true',
                startDate: '07/16/1989',
                firstDay: 1
            });

但是,我想要一点零钱。而不是文本字段,我想显示一个名为“选择日期”的链接,它应该打开日期选择器,并且在选择日期后,需要更新隐藏文本字段的值。

我尝试了不同的方法来做到这一点,但都没有成功。 jQuery专家,你能帮帮我吗?我急切地等待收到解决方案。非常感谢您。

【问题讨论】:

  • 您可能会通过将文本框设置为看起来像超链接来“作弊”。

标签: jquery jquery-ui calendar datepicker


【解决方案1】:

您实际上可以将日期选择器附加到 <input type="hidden" /> 并让链接成为打开它的触发器:

$(function() {
    $('#hiddenDate').datepicker({
        changeYear: 'true',
        changeMonth: 'true',
        startDate: '07/16/1989',
        firstDay: 1
    });
    $('#pickDate').click(function (e) {
        $('#hiddenDate').datepicker("show");
        e.preventDefault();
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js"></script>

<input id="hiddenDate" type="hidden" />
<a href="#" id="pickDate">Select Date</a>

【讨论】:

    【解决方案2】:

    试试这个:

    $('#datepicker').datepicker('hide');
    $('a').click(function () {
        $('#datepicker').datepicker('destroy');
        $('#datepicker').datepicker({
            altField: '#shhh',
            changeYear: 'true',
            changeMonth: 'true',
            firstDay: 1,
            onSelect: function () {
                $('#datepicker').datepicker('destroy');
            }
        })
    });
    

    jsFiddle example

    【讨论】:

      猜你喜欢
      • 2018-10-17
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-27
      • 2018-10-17
      相关资源
      最近更新 更多