dsebywql016137 2013-01-26 12:22
浏览 42
已采纳

jQuery代码无法使用/应用于使用javascript从ajax调用的文件中的元素

Hoping someone can point me in the right direction to an annoying problem.

To summarize before I put the code; I have two files.. a html file with javascript called "bookings.php", and a php file with php "getBookings.php".

The bookings.php file contains all the static information like menus, text etc along with javascript + ajax calls.

I'm calling the getBookings file successfully with javascript/ajax and works well and i don't have any problems with it.

My problem is, that from the file that is loaded (getBookings.php); there are some html elements that i need to toggle. The jQuery for it, I have included in the bookings.php file.. but the jquery won't apply or work when the ajax file is called.

All the contents on getBookings.php gets loaded into an #bookingSummary element.

Now, my code is very long, so i will include all the important parts..

// Usual Head Content, Meta, CSS, jQuery files
// Filters
<script language="javascript" type="text/javascript">
function bookings()
{
    var fltr="";
    if(document.frm.bkid.value != "")
        fltr+='bkid='+document.frm.bkid.value+'&';

    if(document.frm.customer.value != "")
        fltr+='cid='+document.frm.customer.value+'&';

    if(document.frm.site.selectedIndex != 0)
            fltr+='sid='+document.frm.site.value+'&';

    if(document.frm.status.selectedIndex != 0)
        fltr+='st='+document.frm.status.value+'&';

    if(document.frm.supp.selectedIndex != 0)
        fltr+='sup='+document.frm.supp.value+'&';   

    if(document.frm.datefrm.value != "")
        fltr+='dfrm='+encodeURI(document.frm.datefrm.value)+'&dto='+encodeURI(document.frm.dateto.value);   

    document.getElementById('bookingsummary').innerHTML="<br /><p align='center'>    <img src='img/bigloading.gif'></p>";
    url='get/getBookings.php?'+fltr;
    getAjaxRec(url, viewbookings);

}


function viewbookings()
{
    if (xmlHttp.readyState == 4 && (xmlHttp.status==200 ||     window.location.href.indexOf("http")==-1))
    {
        document.getElementById("bookingsummary").innerHTML = xmlHttp.responseText;
    }   
 }

</script>

The Above code is the javascript that all works fine.. the following is the html code that calls the above function - bookings():

<!-- Several Form fields are here..-->
<input type="button" value="Apply Filter"  class="searchBtn" onClick="bookings();">


<!-- Show booking results -->
<div id="bookingsummary"></div>

Now, within the getBookings file there is an html element with a class name "hiddenDetails" that i need to toggle and the Jquery for it is:

<script type="text/javascript"> 
 $(document).ready(function() {

  $('.bookingKeyDetails').click(function()
  {         
        $(this).next('.hiddenDetails').slideToggle('medium");
  });

 });
</script>   

The above code all works apart from the jquery.. here is a snippet from the getBookings.php file.. the part i want to apply the jquery to..

<?php
// A while loop happens then..
$result="           
<div class=\"bookingKeyInfo\">
<div class=\"lb1\"><input type=\"checkbox\" name=sup1x".$rw['id']." ></div>
<div class=\"lb2\"><a href=\"editbooking.php?id=".$rw['id']."\">".$rw['id']."</a>     </div>
<div class=\"lb3\">".date('d/m/Y', strtotime($rw['pickupDate']))." <strong><font     color=\"#FF0000\">".date('H:i', strtotime($rw['pickupTime']))."</font></strong></div>
<div class=\"clearLeft\"></div>
</div>

<div class=\"bookingKeyDetails\">
<div class=\"b-one\"><img style=\"margin-top:9px; padding-bottom: 5px;\"     src=\"$base_dir/img/".strtolower($rw['status']).".png\"></div>
<div class=\"b-two\"><img id=\"isCustomerEmailed\" style=\"margin-top:9px; padding-    bottom: 5px;\" src=\"$base_dir/img/unassigned.png\"></div>
<div class=\"b-three\"><img id=\"isSupplierEmailed\" style=\"margin-top:9px; padding-    bottom: 5px;\" src=\"$base_dir/img/unassigned.png\"></div>
<div class=\"b-four\"><img id=\"isSupplierConfirmed\" style=\"margin-top:9px; padding-    bottom: 5px;\" src=\"$base_dir/img/unassigned.png\"></div>    
<div class=\"b-trip\">".substr($rw['DA'],0,6)."..<strong> to </strong>     ".substr($rw['DB'],0,6)."..</div>
<div class=\"b-pax\">".$rw['pax']." Pax</div>
<div class=\"b-cost\">&euro;$cost </div>
<div class=\"b-supplier\">".substr($sup,0,10)."..</div>

<div class=\"clearLeft\"></div>

<div class=\"hiddenDetails\">More Booking Information...</div>
</div>";

echo $result;
?>

I know this is a very long description of the problem.. so I have tried to give my code exactly as it is..

Could anyone shed some light, as to why the jQuery isn't working.. cz it works separately, just not on any element that is called from getBookings..

  • 写回答

4条回答 默认 最新

  • dongshuo8756 2013-01-26 12:31
    关注

    The problem is, you're binding an event handler with jQuery to bookingKeyDetails before it actually exists on the page. Remember that this element is being added to your html only after the page is loaded, so any actions you make in $(document).ready(function() { will not apply to it.

    Bind the event handler only after you change the html:

    function viewbookings() 
    {
      if (xmlHttp.readyState == 4 && (xmlHttp.status==200 || window.location.href.indexOf("http")==-1))
      {
        document.getElementById("bookingsummary").innerHTML = xmlHttp.responseText;
        $('.bookingKeyDetails').click(function()
        {         
          $(this).next('.hiddenDetails').slideToggle('medium');
        });
      }   
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突