在我的索引頁中,我有3個列,第一列顯示數據庫中的所有類別,第二列顯示單擊類別時的主題,第三列顯示單擊主題時選擇的內容。d在一頁上多個Ajax請求
我在做什麼是,當點擊任何類別時,Ajax請求與該類別相關的所有主題的數據庫,並將其顯示在column1和It Works中,但是當我單擊生成的主題以獲取該內容時主題它無法異步請求,而是它帶我到content.php。使其更清楚,我正在粘貼的index.php和的script.js(所有的JavaScript和jQuery文件被列入header.php文件)
索引頁
<?php
include("tpl/header.php");
?>
<div id="navigation">
<ul id="search_form">
<?php
$cat = Category::find_all();
foreach($cat as $category) {
echo '<li value="';
echo $category->cat_id;
echo '" class="myLi" "><a href="subject.php?id=';
echo $category->cat_id;
echo'">';
echo $category->category;
echo '</a></li>';
}
?>
</ul>
</div>
<div id="content1">
<h2>Subjects</h2>
<!-- works -->
</div>
<div id="content2">
<h2>Content</h2>
<!-- doesnt work -->
</div>
<?php
include_layout_template("footer.php");
?>
的js代碼
$(document).ready(function() {
$(".myLi").click(function(){
var id = this.value;
ajax.open('get', 'subject.php?id=' + encodeURIComponent(id));
ajax.onreadystatechange = function() {
// Pass it this request object:
handleResponse(ajax);
}
ajax.send(null);
return false; // So form isn't submitted.
});
$(".myLi1").click(function(){
var cid = this.value;
ajax.open('get', 'content.php?id=' + encodeURIComponent(cid));
ajax.onreadystatechange = function() {
// Pass it this request object:
handleResponse1(ajax);
}
ajax.send(null);
return false; // So form isn't submitted.
});
function handleResponse(ajax) {
// Check that the transaction is complete:
if (ajax.readyState == 4) {
// Check for a valid HTTP status code:
if ((ajax.status == 200) || (ajax.status == 304)) {
// Put the received response in the DOM:
var results = document.getElementById('content1');
results.innerHTML = ajax.responseText;
// Make the results box visible:
results.style.display = 'block';
}
} // End of readyState IF.
} // End of handleResponse() function.
function handleResponse1(ajax) {
// Check that the transaction is complete:
if (ajax.readyState == 4) {
// Check for a valid HTTP status code:
if ((ajax.status == 200) || (ajax.status == 304)) {
// Put the received response in the DOM:
var results1 = document.getElementById('content2');
results1.innerHTML = ajax.responseText;
// Make the results box visible:
results1.style.display = 'block';
}
} // End of readyState IF.
} // End of handleResponse() function.
});
..只有當我點擊索引頁面上的主題時,纔會失敗,當我導航到subject.php並單擊鏈接時,它會異步提供結果。所以我的問題是我如何查詢單個頁面上的多個Ajax調用(即在我的情況下「index.php」)..任何幫助?
焦慮問題,但您可能對[此功能]感興趣(http://api.jquery.com/load/) – greg0ire 2011-12-17 16:11:59
是的!感謝這個:-) – XoR 2011-12-17 16:21:45