【问题标题】:Jquery Datepicker not displayed on first clickJquery Datepicker在第一次点击时不显示
【发布时间】:2015-09-02 10:38:21
【问题描述】:

我正在使用 JQueryUI 日期选择器,当第一次单击日历图像时,日历未显示。但是,当回发发生时,会显示日历。

我使用下面是代码sn-p来初始化datepicker:

$(function() {
      var date = new Date();
      var currentMonth = date.getMonth();
      var currentDate = date.getDate();
      var currentYear = date.getFullYear();

      $("#ReportDate").datepicker({
        showOn: "both",
        buttonImage: "calendar.png",
        buttonImageOnly: true,
        buttonText: "Select date",
        maxDate: new Date(currentYear, currentMonth, currentDate)
      });
   });
  .ui-datepicker {
            font-size: 8pt !important;
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker</title>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>

</head>

<body>
<form id="form1" runat="server">
<div class="demo">
<b>Date:</b> <input type="text" id="ReportDate" />
</div>
</form>
</body>
</html>

谁能帮忙。我真的被困很久了。

【问题讨论】:

  • 你能发一个小提琴吗
  • 代码,fiddle 或 plunker。提供一些东西
  • 您是在通知我们还是在询问?请发布您的代码和问题
  • 您提供的代码小提琴不起作用!在您钓鱼为其他人发布代码之前,请确保Run 代码!此外,您发布的代码在语法上是错误的!
  • 您的代码小提琴不起作用!检查!

标签: jquery datepicker jquery-ui-datepicker


【解决方案1】:

据我的理解,这个问题通常发生在输入字段是页面的第一个输入,并且在dom初始化时已经处于focus状态。所以我们需要再次点击该字段来初始化focus和datepicker。所以最好在点击功能上初始化日期选择器。

$('#datepicker').click(function(){
    $('#datepicker').datepicker('show');
});

offcouse fiddle 或 plunker demo of question 将有助于提供更多答案。

【讨论】:

  • 即使我尝试过,但除非在页面上发生回发,否则它不起作用。
【解决方案2】:

您是否在页面加载时初始化您的日期选择器?

<script>
  $(function() {
    $( "#datepicker" ).datepicker();
  });
</script>

编辑

可能引用错误。 试试这个:https://jsfiddle.net/ddan/8vjtzru4/1/

【讨论】:

  • @Vineeta 我尝试了很多东西仍然无法正常工作。我认为点击事件在页面加载时并没有得到附加。我无法附上图片,否则会让你们理解的很清楚:(
  • 不,它仍然是同样的问题。我将我的 Jquery 和 CSS 引用替换为您提供的引用。还。我的测试框是 ASp.net 文本框:
  • 这很有趣。在 JSFiddle 中它可以工作。你是不是替换了JS代码。在您的示例中,末尾缺少 '});'
  • 你的因为语法错误无法运行,函数没有关闭。
  • 粘贴到小提琴时是一个错字。我的代码中有有效的语法@DDan
【解决方案3】:

我认为你的脚本标签有误

我已经更新了你的代码Here

检查此链接,让我知道它是否有效。

【讨论】:

  • 即使那样它在我的页面上也不起作用。如果我将它放在 html 页面上,它似乎工作正常。但是当它必须对回发做一些事情时,它不能在 .aspx 页面上工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
  • 2020-01-05
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
相关资源
最近更新 更多