0

我在 ajax 调用中加载我的 datepicker 脚本,它可以正常工作并按应有的方式加载 datepicker。我遇到的问题是,当我点击一天时,它不会触发 onclick 功能。

如果我在单独的窗口中自行加载日期选择器页面,那么它可以正常工作

AJAX 调用

function dates(str2){

  var event = findSelection("event");
  if (str2 == "")
  {
    document.getElementById("dates").innerHTML = "";
    return;
  }
  if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
  }
  else
  {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange = function()
  {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
      document.getElementById("dates").innerHTML = xmlhttp.responseText;
      $("#datepicker").datepicker();
    }
  }


  xmlhttp.open("GET", "dates.php?event=" + 
    event + "&type=" + document.getElementById("type").value, true);

  xmlhttp.send();

}

日期.php

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>jQuery UI Datepicker - Display inline</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css" />
  <script>
  $(function () {
      $('#datepicker').datepicker({
          dateFormat: 'dd-mm-yy',
          numberOfMonths: 1,
          onSelect: function (selected, evnt) {
              alert("I am an alert box!");
          }
      });
  });      </script>
</head>
<body>

Date: <div id="datepicker"></div>


</body>
</html>
4

1 回答 1

0

也许是这样的

dates.php(删除除此之外的所有内容)

Date: <div id="datepicker"></div>

然后你的 js 代码(将 redystate 更改函数更改为):

  xmlhttp.onreadystatechange = function()
  {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
      document.getElementById("dates").innerHTML = xmlhttp.responseText;
      $('#datepicker').datepicker({
          dateFormat: 'dd-mm-yy',
          numberOfMonths: 1,
          onSelect: function (selected, evnt) {
              alert("I am an alert box!");
          }
      });
    }
  }

或者因为您无论如何都在使用 jQuery,请将日期函数更改为

function dates(str2){
  var event = findSelection("event");
  $("#dates").load("dates.php?event=" + 
    escape(event) + "&type=" + escape($("#type").val()));
  $('#datepicker').datepicker({
     dateFormat: 'dd-mm-yy',
     numberOfMonths: 1,
     onSelect: function (selected, evnt) {
        alert("I am an alert box!");
     }
 });
}
于 2013-10-07T18:23:30.813 回答