2

カレンダー選択入力のあるhtmlページがあります。localhost で開くと正常に動作していますが、php ページで ajax を介して同じコードを呼び出していますが、動作していません。以下はHTMLコードです

<html>
   <head>
   <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
   <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
   <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
   <script>
      $(function() {
      $( "#datepicker" ).datepicker({
            numberOfMonths: 3,
            showButtonPanel: true
      });
      });
   </script>
   </head>

   <body>
   <input type="text" id="datepicker" />
   </body>
   </html>

PHPページで上記のコードを呼び出し、そのページをajax経由でロードすると、上記のコードが機能しません。

私は2つのphpファイルdelete.php、deleteDateCalender.phpを使用しています

delete.php のコード

<html>
<head>
<title>test</title>

 <script>

        function deleteCalDateAjax()
        {

        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)
             {
                 //alert(xmlhttp.responseText);
                 document.getElementById("calenderDIV").innerHTML=xmlhttp.responseText;
                 //alert(xmlhttp.responseText);
             }
          }

            xmlhttp.open("GET","deleteDateCalender.php",true);
            xmlhttp.send(null); 
        }

 </script>

</head>

<body>

                        <form>
                            <input type="button" value="test" onclick="deleteCalDateAjax();" />
                            <div id="calenderDIV">
                            &nbsp;
                            </div>
                        </form>
</body>
</html>

deleteDateCalender.php のコード

<input type="text" id="datepicker" />


  <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
 <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
 <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
 <script>
 $(function() {
      $( "#datepicker" ).datepicker({
            numberOfMonths: 3,
            showButtonPanel: true
      });
 });
 </script>

delete.php のテスト ボタンをクリックすると、deleteDateCalender.php が呼び出されます。

上記のコードで何が間違っているか分かりますか?

どんな助けでも大歓迎です。

4

1 に答える 1

0

$(関数() { ... }); ドキュメントの読み込みが完了するとトリガーされます。そのコードを AJAX 経由でロードすると、ドキュメントはすでにロードされているため、トリガーされることはありません。

あなたの最善の策は、AJAX からページに HTML を追加し終わった後に datepicker() スクリプトを開始することです。

delete.php コードは次のようになります: (jQuery を使用しているかのように、他の XMLHttpRequest JavaScript をすべて削除し、すべてを処理し、すべてのブラウザーで動作するようにします)

<html>
<head>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.8.3.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript">
function deleteCalDateAjax() {
   // Call the php page to get the HTML
   $.ajax({
      url: 'deleteDateCalender.php',
      success: function(responseHTML) {
         // Set the HTML somewhere on the page - note: if you are returning a full page of HTML you shouldn't include all the <html><body> tags etc...
         $("#calenderDIV").html(responseHTML);
         // Now that your HTML is available in the DOM you can initiate the datepicker()
         $("#datepicker").datepicker({
           numberOfMonths: 3,
           showButtonPanel: true
         });
         $("#datepicker").datepicker("show");
      }
   });
}
</script>
</head>
<body>
<h1>Example</h1>
<input type="button" onclick="deleteCalDateAjax();" value="Test" />
<div id="calenderDIV">this text will be replaced with HTML from AJAX call</div>
<input name="DateButton" type="button" id="DateButton" value="Open Calander" onclick="$('#datepicker').datepicker('show');" />
</body>
</html>

deleteDateCalender.php のコード (なぜこれを行うのか完全にはわかりませんが、計画があることは確かです)

<input type="text" id="datepicker" />

それは役に立ちますか?

于 2013-02-26T06:19:25.070 に答える