简体   繁体   中英

jquery calender not working via ajax page load in php

I have html page with calender select input. It is working fine when I open in localhost but I call same code in php page via ajax it is not working. Below is HTML Code

<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>

If I am call above code in php page and loading that page via ajax the above code not working.

I am using 2 php file delete.php, deleteDateCalender.php

code for 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>

code for 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>

deleteDateCalender.php called on click of test button in delete.php

Can you see what is wrong in the above code?

Any help will be appreciated.

The $(function() { ... }); is triggered when the document has finished loading. If you load that code via AJAX then the document has already been loaded and will therefor never get triggered.

Your best bet would be to initiate the datepicker() script AFTER you have finished adding the HTML from AJAX into the page.

Your delete.php code would look something like this: (I removed all that other XMLHttpRequest JavaScript as if you are using jQuery it handles everything for you and ensures it works across all browsers)

<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>

code for deleteDateCalender.php (I'm not entirely sure why you would do this but I'm sure you have a plan)

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

Does that help?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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