【问题标题】:Jquery Datepicker in AJAX pageAJAX页面中的Jquery Datepicker
【发布时间】:2011-12-02 14:19:23
【问题描述】:

我有 2 页。 1 是 jQuery 的 datepicker 演示,另一个是用于加载 datepicker 演示页面的 AJAX 页面。当我直接访问 datepicker 页面时,日期选择器工作正常,如示例中所示。但是当我尝试使用 ajax 调用加载它时,选择器似乎根本不起作用。

这里是main.php页面代码

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("myDiv").innerHTML=xmlhttp.responseText;
    }
}

xmlhttp.open("GET","development-bundle/demos/datepicker/default.html",true);
xmlhttp.send();
}
</script>
</head>

<body>
    <div id="myDiv"><h2>Let AJAX change this text</h2></div>
    <button type="button" onclick="loadXMLDoc()">Change Content</button>    
</body>
</html>

这是 datepicker 页面代码(它与 jquery 中的演示代码相同)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>jQuery UI Datepicker - Default functionality</title>
    <link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
    <script src="../../jquery-1.6.2.js"></script>
    <script src="../../ui/jquery.ui.core.js"></script>
    <script src="../../ui/jquery.ui.widget.js"></script>
    <script src="../../ui/jquery.ui.datepicker.js"></script>
    <link rel="stylesheet" href="../demos.css">
    <script>
        $(function() {
            $( "#datepicker" ).datepicker();
        });
    </script>
</head>
<body>
    <div class="demo">    
        <p>Date: <input type="text" id="datepicker"></p>    
    </div><!-- End demo -->

    <div class="demo-description">
        <p>The datepicker is tied to a standard form input field.  Focus on the input (click, or use the tab key) to open an interactive calendar in a small overlay.  Choose a date, click elsewhere on the page (blur the input), or hit the Esc key to close. If a date is chosen, feedback is shown as the input's value.</p>
    </div><!-- End demo-description -->    
</body>
</html>

我真的迷路了。日期选择器页面不能与 AJAX 一起使用。希望有人可以在这里提供帮助。

【问题讨论】:

  • 您可能会遇到一些严重的问题,因为将整个 HTML 文档填充到现有页面上的 &lt;div&gt; 中是无效的。

标签: javascript jquery ajax


【解决方案1】:

试试这个:

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("myDiv").innerHTML=xmlhttp.responseText;
     $( "#datepicker" ).datepicker();
    }
  }
xmlhttp.open("GET","development-bundle/demos/datepicker/default.html",true);
xmlhttp.send();
}
</script>

您需要在收到 ajax 请求后调用它

【讨论】:

  • 我遇到了同样的问题并尝试了您的解决方案,但没有奏效。我从 OP 看到的唯一不同是我的 ajax 调用页面(OP 的 default.html)没有 HTML 文档代码(&lt;head&gt;&lt;body&gt; 等)我想知道您的解决方案是否与这种情况相关?
  • 解决了。我的主页上还有一个日期选择器,并且我的两个日期选择器 ID 设置相同。在这里找到答案:stackoverflow.com/a/11793645/1728650
  • 值得注意的是,“您需要在收到 ajax 请求后调用它”具体意味着将 $( "#datepicker" ).datepicker(); 放在 document.getElementById("myDiv").innerHTML=xmlhttp.responseText; 之后,否则它将不起作用。我为此挠了一阵子。
【解决方案2】:

这就是你所追求的

$('#datepicker').bind('focus', function() {
  $(this).datepicker();
});

【讨论】:

    【解决方案3】:

    你需要在main.php中有这样的

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

    在日期选择器页面中

    <input type="text" id="datepicker" onclick="activateDatePicker()">
    

    您应该在 main.php 中包含 javascript

    这应该不是第一次,而是第二次尝试选择日期

    【讨论】:

    • 是的,它正在第二次工作。任何想法如何让它第一次工作?
    • 也可以像这样激活 document.getElementById("myDiv").innerHTML=xmlhttp.responseText;激活日期选择器();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    相关资源
    最近更新 更多