[英]Only select a <div> from an external page with Ajax/PHP
我有一个小问题。 因此,在<select>
检测到更改之后,我正在使用Ajax将另一页的内容加载到<div>
<select>
。 但事实是,我只想从该外部页面中将特定的<div>
加载到我的主页中。 这是主页的代码:
<script type="text/javascript">
$(function(){
createListeSelectWithDefault("categorie", <?php echo getJsCategorieListe()?>);
});
function showListes(str)
{
if (str=="")
{
document.getElementById("txtHint").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("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","liste_result?q="+str,true);
xmlhttp.send();
}
</script>
<div id="content">
<div id="bloc">
<div id="title">Combiner</div>
<div class="mx">
<form name="new_combiner">
<select id="categorie" name="categorie" onchange="showListes(this.value)"></select>
</form>
</div>
<div class="mx">
<div id="txtHint">
</div>
</div>
</div>
</div>
这是我的外部页面:
<html>
<div> some other kind of text, no need to post it here, but can't take it away</div>
<div id="mainDiv">
<?php
$q=$_GET["q"];
$liste = getListeByCategorie($q);
echo "<table border='1'>
<tr>
<th>Titre</th>
<th>Vues</th>
</tr>";
foreach($liste as $row)
{
echo "<tr>";
echo "<td>" . $row->titre() . "</td>";
echo "<td>" . $row->vue() . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</div>
<div> some other kind of text, no need to post it here, but can't take it away</div>
这样就可以了,仅此而已。 我该怎么办? 我只希望将<div id="mainDiv">
加载到我的页面中。
请更彻底地研究API。 这个特殊的用例已经涵盖了。
在这里看: http : //api.jquery.com/load/
(请参见页面上标题为“加载页面片段”的部分)
从API页面:
$('#result').load('ajax/test.html #container');
这将加载只在div id为容器,从外部页的test.html,与请求页面上的ID结果股利。 没有比这更简单的了(根据您的要求更改外部文档选择器)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.