2013-10-16 14 views
0

當我在test1.php上做出選擇時,它將onchange =「showbookings(this.value)的值傳遞給test2.php,然後返回從mysql調用中填充的html代碼。所有這一切都很完美ajax不允許html類的點擊函數運行

我遇到的問題是類功能contactus和status_button不再在email.js中工作當我點擊contactus ...什麼也沒有發生當我點擊status_button頁面刷新回未選中的框我正在尋找關於如何在選擇和html呈現後如何讓類功能工作的建議。他的HTML,所以我知道他們的工作。我只是不明白爲什麼他們不再使用這個選擇。

<a id='email1' href='#' class='contactus'><img src='emailbutton.jpg' title='Email' border='0' height='24' width='24'></img></a> 

<div id='messageA1'><a id='1' href='' class='status_button' title='C1'><div id='messageB1'><img src='closebutton.jpg' title='Order Status' border='0' height='24' width='24'></img></div></a></div> 

test1.php

<?php 
include('connection.php'); 

echo '<html> 
<head> 
    <link rel="stylesheet" href="booking.css" /> 
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> 
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
    <script src="http://code.jquery.com/jquery-latest.min.js"></script> 
    <script src="email.js"></script> 
</head> 
<body> 
<a href=""><img src="/images/logo.png" height="123" width="298"></a> 
    <br/> 
    <form style="width: 1100px"> 
     <select name="bookings" onchange="showbookings(this.value)"> 
      <option value="">Select Bookings To See:</option> 
      <option value="1">All Booking</option> 
      <option value="2">Open Bookings</option> 
      <option value="3">Closed Bookings</option> 
     </select> 
    </form> 
    <div id="txtBookings"></div> 
</body> 
</html>'; 
?> 

test2.php運行並返回以下HTML從MySQL填寫回

<div id="txtBookings"></div> 

上test1.php

<form id='form1' style='width: 1100px'> 
    <table> 
     <th>Id</th> 
     <th>First Name</th> 
     <th>Last Name</th> 
     <th>Email</th> 
     <th>Phone</th> 
     <th>Address</th> 
     <th>City</th> 
     <th>State</th> 
     <th>Zip</th> 
     <th>Adults</th> 
     <th>Kids</th> 
     <th>Pets</th> 
     <tr> 
      <td> 
       <input type='text' id='id1' size='3' readonly='readonly' value='1'> 
      </td> 
      <td> 
       <input type='text' id='fname1' size='8' value='Bob'> 
      </td> 
      <td> 
       <input type='text' id='lname1' size='13' value='Smith'> 
      </td> 
      <td> 
       <input type='text' id='email1' size='25' value='[email protected]'> 
      </td> 
      <td> 
       <input type='text' id='phone1' size='10' value='(555)-555-5555'> 
      </td> 
      <td> 
       <input type='text' id='addy1' size='22' value='123 Main St'> 
      </td> 
      <td> 
       <input type='text' id='city1' size='10' value='Anywhere'> 
      </td> 
      <td> 
       <input type='text' id='state1' size='1' value='CA'> 
      </td> 
      <td> 
       <input type='text' id='zip1' size='2' value='99999'> 
      </td> 
      <td> 
       <input type='text' id='adults1' size='2' value='2'> 
      </td> 
      <td> 
       <input type='text' id='kids1' size='1' value='0'> 
      </td> 
      <td> 
       <input type='text' id='pet1' size='1' value='0'> 
      </td> 
     </tr> 
    </table> 
    <table> 
     <th>Level</th> 
     <th>Check In</th> 
     <th>Check Out</th> 
     <th>Booked</th> 
     <tr> 
      <td> 
       <input type='text' id='level1' size='2' value='Upper'> 
      </td> 
      <td> 
       <input type='text' id='chkin1' size='7' value='11/21/2013'> 
      </td> 
      <td> 
       <input type='text' id='chkout1' size='7' value='11/24/2013'> 
      </td> 
      <td> 
       <input type='text' id='booked1' size='7' value='10/02/2013'> 
      </td> 
      <td><a id='edit1' href='' class='edit_button' title=''><img src='editbutton.jpg' title='Submit Edit' border='0' height='24' width='24'></img></a> 
      </td> 
      <td><a id='email1' href='#' class='contactus'><img src='emailbutton.jpg' title='Email' border='0' height='24' width='24'></img></a> 
      </td> 
      <td> 
       <div id='messageA1'><a id='1' href='' class='status_button' title='C1'><div id='messageB1'><img src='closebutton.jpg' title='Order Status' border='0' height='24' width='24'></img></div></a> 
       </div> 
      </td> 
     </tr> 
    </table> 
</form> 

email.js

$(document).ready(function() { 
    $(".contactus").click (function() { 
// alert("email"); 
     var element = $(this); 
     var J = element.attr("id"); 
     var email = document.getElementById(J).value; 
     $("#result").html("<h3>Loading</h3>").css("display", "block"); 
     findCenter($("#result")); 
     $.get("email.php?email=" + email, function (data) { 
      $("#result").html(data); 
      findCenter($("#result")); 
      FocusOnInput(); 
      $("#snd").click(function() { 
       var subject = document.getElementById("subject").value; 
       var addy = document.getElementById("addy").value; 
       var comments = document.getElementById("comments").value; 
       $("#result").append("<br /><br /><div><i>Sending...</i></div>"); 
       $.post("email.php", { 
        mode: "snd", 
        subject: subject, 
        addy: addy, 
        comments: comments 
       }, function (data) { 
        $("#result").html(data); 
        findCenter($("#result")); 
       }); 
      }); 
      $("#cnx").click(function() { 
       $.post("email.php", { 
        mode: "cnx" 
       }, function (data) { 
        $("#result").html(data); 
        findCenter($("#result")); 
       }); 
      }); 
     }); 
    }); 
}); 

$(document).ready(function() { 

    $(".status_button").click (function() { 
// alert("status"); 
     var element = $(this); 
     var I = element.attr("id"); 
     var id = $("#id" + I).val(); 
     var sname = $(this).attr("title"); 
     $.post("openclose.php", { 
      id: id, 
      sname: sname 
     }, 

     function (data) { 
      var response = (data).split(";", 3); 
      $("#messageA" + I).innerhtml = (response[0]); 
      $("#messageA" + I).hide(); 
      $("#messageA" + I).fadeIn(1500); 
      $("#messageB" + I).html(response[1]); 
      $("#messageB" + I).hide(); 
      $("#messageB" + I).fadeIn(1500); 
      document.getElementById(I).value = (response[2]); 
      document.getElementById(I).title = (response[2] + I); 
     }); 
     return false; 
    }); 
}); 


function showbookings(str) { 
    if (str === "") { 
     document.getElementById("txtBookings").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("txtBookings").innerHTML = xmlhttp.responseText; 
     } 
    }; 
    xmlhttp.open("GET", "test2.php?q=" + str, true); 
    xmlhttp.send(); 
} 

回答

0

您需要重新安裝單擊事件監聽器加載動態內容之後。綁定僅在偵聽器已連接時已存在於頁面上的元素髮生。在調用on()之後,偵聽器不會附加到添加到DOM的元素。

在您進行ajax調用之後,您需要執行$(".status_button").on('click',function(){....});以將點擊事件綁定到任何新的.status_button元素。

從jQuery文檔爲on()

的事件處理程序僅結合到當前選擇的元素;它們必須在代碼調用.on()時存在於頁面上。

+0

adear11 - 感謝您的回覆。如何以及在哪裏重新附加點擊事件偵聽器?你寫道:「在ajax調用後...將點擊事件綁定到任何新的.status_button元素。」在ajax調用之前沒有.status_button元素,所以它們都是新的。任何建議將不勝感激。謝謝,Kurt – stapuff

+0

你應該在你的'onreadystatechange'函數中完成它,在你賦值innerHTML之後 – adear11

+0

你有腳本例子嗎?謝謝,Kurt – stapuff

1

綁定點擊事件處理程序這樣,使用委託的事件處理程序,以便他們將動態生成HTML元素的工作:

$('body').on('click', '.status_button', function() { 
    // your code here 
}; 

你有它的方式,(只有$(".status_button").click(...)結合) ,當點擊事件處理程序被綁定時,這些元素還不存在,這就是爲什麼它不起作用。代理表單中的on()方法即使對於綁定處理程序時尚未創建的元素也可以工作。

爲了獲得更好的性能,您還可以將它附加到綁定事件時已經存在的最接近的祖先.status_button(將'body'替換爲該祖先的選擇器)。

從文檔(再往下放事件處理程序):

當提供一個選擇器,該事件處理程序被稱爲 委派。當事件發生時,直接上 結合的元件,但僅用於後代(內元件) 匹配選擇處理程序不被調用。 jQuery將事件從事件目標 中的事件冒泡到處理程序所附的元素(即,最內層到最外層元素 ),併爲匹配選擇器的路徑上的任何元素運行處理程序。

http://api.jquery.com/on/

+0

mayabelle - 我很感謝您的回覆。我嘗試過.on和.live,並沒有得到任何不同的結果。 Kurt – stapuff

+0

mayabelle - 我也試過.bind謝謝,庫爾特 – stapuff

+0

不是,但你有你需要的東西。所有你需要做的就是將'$(document).ready()'函數中當前'$('。status_button')。click()'方法調用移動到'document.getElementById(「txtBookings」 ).innerHTML = xmlhttp.responseText' – adear11

0

我接受了建議,並將這些功能加在一起。以下是我所做的。所有功能都按照應有的方式工作。

function showbookings(str) { 
    if (str === "") { 
     document.getElementById("txtBookings").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("txtBookings").innerHTML = xmlhttp.responseText; 

     $(".contactus").on('click', function() { 
     var element = $(this); 
     var J = element.attr("id"); 
     var email = document.getElementById(J).value; 
     $("#result").html("<h3>Loading</h3>").css("display", "block"); 
     findCenter($("#result")); 
     $.get("email.php?email=" + email, function (data) { 
      $("#result").html(data); 
      findCenter($("#result")); 
      FocusOnInput(); 
      $("#snd").click(function() { 
       var subject = document.getElementById("subject").value; 
       var addy = document.getElementById("addy").value; 
       var comments = document.getElementById("comments").value; 
       $("#result").append("<br /><br /><div><i>Sending...</i></div>"); 
       $.post("email.php", { 
        mode: "snd", 
        subject: subject, 
        addy: addy, 
        comments: comments 
       }, function (data) { 
        $("#result").html(data); 
        findCenter($("#result")); 
       }); 
      }); 
      $("#cnx").click(function() { 
       $.post("email.php", { 
        mode: "cnx" 
       }, function (data) { 
        $("#result").html(data); 
        findCenter($("#result")); 
       }); 
      }); 
     }); 
    }); 
    $(".status_button").on('click', function() { 
     var element = $(this); 
     var I = element.attr("id"); 
     var id = $("#id" + I).val(); 
     var sname = $(this).attr("title"); 
     $.post("openclose.php", { 
      id: id, 
      sname: sname 
     }, 
     function (data) { 
      var response = (data).split(";", 3); 
      $("#messageA" + I).innerhtml = (response[0]); 
      $("#messageA" + I).hide(); 
      $("#messageA" + I).fadeIn(1500); 
      $("#messageB" + I).html(response[1]); 
      $("#messageB" + I).hide(); 
      $("#messageB" + I).fadeIn(1500); 
      document.getElementById(I).value = (response[2]); 
      document.getElementById(I).title = (response[2] + I); 
     }); 
     return false; 
    }); 
     } 
    }; 
    xmlhttp.open("GET", "test2.php?q=" + str, true); 
    xmlhttp.send(); 
}