我正在使用jQuery從用戶輸入搜索詞彙時從php類中加載搜索結果。問題在於,jQuery首次在主頁面上運行並執行所需功能,但是當用戶輸入搜索關鍵字並且結果通過ajax從同一頁面上的類文件加載時,jQuery不會執行功能需要。當通過AJAX調用頁面時jQuery不會運行第二次
我想要做的是,當搜索結果加載到主頁面時,它應該只顯示Quote和作者,而不是阿拉伯文和參考。當用戶點擊報價時,它應該像一個手風琴家一樣,切換阿拉伯文和參考的div容器。
但是,儘管jQuery可以很好地從類文件中加載結果,但它在加載搜索結果時不會執行隱藏阿拉伯文和引用的容器,也不會在用戶單擊引用顯示容器,如螢火蟲顯示。
的index.php:
<script type="text/javascript">
$(function() {
$(".bsearch")
.keydown(function() {
//create post data
var postData = {
"search" : $(this).val()
};
//make the call
$.ajax({
type: "POST",
url: "quotes_in.php",
data: postData, //send it along with your call
success: function(response){
$("#left").html(response);
}
});
}),
$("div#smore").hide();
//toggle the componenet with class msg_body
$("p#s_quotes").click(function()
{
$(this).next("div#smore").slideToggle(200);
}),
});
</script>
<!-- Begin Left Column -->
<div id="left">
</div>
<!-- End Left Column -->
Quotes_in.php:
include_once "inc/class.quotes.inc.php";
$quotes = new Quotes($db);
if (isset($_POST['search']))
$quotes->searchQuotes();
在類文件中的函數,用於格式化搜索:
---SQL QUERY for SEARCH---
public function formatSearch($row, $search)
{
return "<div class=\"swrap\">"
. "<p id=\"s_quotes\"> " . $cQuote . " </p>"
. "<div id=\"smore\"><p id=\"s_arabic\">" . $this->h($row['cArabic']) . "</p>"
. "<p id=\"s_reference\"><span class=\"source\">Source:</span> " . $this->h($row['vReference']) . "</p></div>"
. "<p id=\"s_author\"><b>-</b> " . $this->h($row['vAuthor']) . "</p></div>";
}
啊,感謝您的解釋,這是有道理的 – input 2010-08-13 13:13:56