jquery datepicker无法处理输入点击

时间:2014-10-22 07:56:38

标签: jquery jquery-ui datepicker

我知道这是一种愚蠢/轻微的错误。但我无法找到它。

我写了这里提到的相同代码http://jqueryui.com/datepicker/#default,但仍然没有。

代码:

<input type="text" id="hasDatepicker" class="fromDate large hasDatepicker" maxlength="10" placeholder="dd-mm-yyyy" autocomplete="off">

JS:

$(".hasDatepicker").datepicker();

小提琴:http://jsfiddle.net/mkcb5b0e/

5 个答案:

答案 0 :(得分:4)

原因是hasDatepicker是jQuery UI datepicker使用的内部类,用于确定是否已为所选元素初始化了datepicker小部件,如果是,则它不会执行任何操作。

因此,在您的情况下,由于input已经有了类,.datepicker()调用并没有做任何事情,假设窗口小部件已经初始化。

&#13;
&#13;
$(".hasDatepicker2").datepicker();
&#13;
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/redmond/jquery-ui.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.js"></script>

<input type="text" id="hasDatepicker" class="fromDate large hasDatepicker2" maxlength="10" placeholder="dd-mm-yyyy" autocomplete="off">
&#13;
&#13;
&#13;

答案 1 :(得分:2)

正确的代码 -

HTML代码 - (从类中删除hasDatepicker)

<input type="text" id="hasDatepicker" class="fromDate large" maxlength="10" placeholder="dd-mm-yyyy" autocomplete="off">

jQuery Code-(将#hasDatepicker作为选择器)

$("#hasDatepicker").datepicker();

工作小提琴 - http://jsfiddle.net/Ashish_developer/m3adjs1r/

答案 2 :(得分:1)

您正在使用ID,因此您必须像$("#hasDatepicker").datepicker();一样打电话 Dot用于课程。 #用于id。您还需要加载jquery UI脚本。 Datepicker是一个UI元素。同样给id&amp;类同名。

来自jquery的完整工作代码

 <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>
</body>
</html>

答案 3 :(得分:1)

选择1 :更改您的班级名称&#34; hasDatepicker&#34;换个名字。

选择2 :删除班级名称&#34; hasDatepicker&#34;并按身份选择。

$("#hasDatepicker").datepicker();

答案 4 :(得分:0)

你的JS中有一个类尝试制作它和ID:

$("#hasDatepicker").datepicker();