0

少し問題があります。<div>そのため、で変更が検出された後、Ajaxを使用して別のページのコンテンツをにロードしてい<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">を自分のページにロードしたいだけです。

4

1 に答える 1

1

APIをもっとよく勉強してください。この特定のユースケースはすでにカバーされています。

ここを見てください: http://api.jquery.com/load/
(「ページフラグメントの読み込み」というタイトルのページのセクションを参照してください)

API ページから:

$('#result').load('ajax/test.html #container');

これにより、外部ページ test.html から id コンテナを持つ divのみが、要求ページの id 結果を持つ div に読み込まれます。これより簡単なことはありません (必要に応じて外部ドキュメント セレクターを変更してください)。

于 2013-03-08T18:13:57.840 に答える