简体   繁体   English

无法使用JavaScript在表单上显示Datepicker

[英]Can't get the Datepicker to show on form using javascript

I'm trying to add Datepicker to my online form as part of my student assessment. 我正在尝试将Datepicker添加到我的在线表单中,作为学生评估的一部分。 I'm very new to all this and I'm having issues . 我对这一切都是新手,我遇到了问题。 I've downloaded and linked to my jquery UI and I've typed the code as per the jquery UI website states but the calendar doesn't pop up on my form when I click in the Date field like it suggests, I've also taken examples from this site but I still can't get it to work. 我已经下载并链接到我的jquery用户界面,并且按照jquery用户界面网站的状态输入了代码,但是当我按日期显示时,日历没有弹出,但是我也想在日历表中显示日历以这个网站为例,但我仍然无法使它正常工作。

<head>
  <link href="javascript/jquery-ui-1.10.3.custom/css/south-street/jquery-ui-1.10.3.custom.css" rel="stylesheet">
  <script src="javascript/jquery-ui-1.10.3.custom/js/jquery-1.9.1.js"></script>
  <script src="javascript/jquery-ui-1.10.3.custom/js/jquery-ui-1.10.3.custom.js"></script>

  <script>  
  function validate(){
  date = document.getElementById("datePicker").value;

  errors = "";

    if (date == ""){
      errors += "Please supply a valid DOB \n";
    }
  }
  </script>
</head>

<body>   
  <form name= "myform" method="post" action="" class="booking">
  <fieldset>
    <div>
      <label for="datePicker" class="fixedwidth">Date</label>
      <input type="text" name="datePicker" id="datePicker"/>
    </div>
  </fieldset>
</body>

您需要在ready或load函数中调用代码,或将其插入函数并在主js文件中调用它。

Need to call datepicker on body load. 需要在身体负荷时调用datepicker。

<script type="text/javascript">
$(document).ready(function(){
    $("#datePicker").datepicker();
});
</script>
 <script>  
$(document).ready(function(){
    $("#datePicker").datepicker();
});

  function validate(){
  date = document.getElementById("datePicker").value;

  errors = "";

    if (date == ""){
      errors += "Please supply a valid DOB \n";
    }
  }
  </script>

You have to have some jQuery code to initialize the datepicker, like 您必须具有一些jQuery代码才能初始化datepicker,例如

    <script type="text/javascript">
    $(function(){
        $('#datePicker').datepicker();
    });
    </script>
You are missing this code in your script
$(function() {
    $( "#datepicker" ).datepicker();
  });

First initialize the datepicker UI method. 首先初始化datepicker UI方法。

$(document).ready(function(){
    $("#datePicker").datepicker();
});

Since you are using jQuery, you get the value like 由于您使用的是jQuery,因此获得的值如下

$("#datePicker").val();

Update: To get the value after selecting it from datepicker 更新:datepicker选择值后获取值

$(document).ready(function () {
    $("#datePicker").datepicker({
        onSelect: function (date) {
            alert(date);
        }
    });
});

JSFiddle JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM