2014-04-28 113 views
0

我能夠從第二頁得到結果,但問題是我得到的是HTML輸出中的所有內容,我只希望它的某些部分...
我已經大約使用jQuery和JavaScript的代碼行的1K,並通過從ajax加載整個頁面,它只是打破了整個應用程序...
第二頁的內容是動態的,它有javascript和jquery,它我不需要這樣做的最終結果,但我確實得到的過程...所以..使用Ajax獲取DIV內容

secondpage.php只是一個基於我的第一頁的表單,所以我的「Ajax」是在「main_page .php「像這樣...

function getXMLHTTP() { //function to return the xml http object 
    var xmlhttp=false; 
    try{ 
     xmlhttp=new XMLHttpRequest(); 
    } 
    catch(e) { 
     try{ 
      xmlhttp= new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     catch(e){ 
      try{ 
       xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
      } 
      catch(e1){ 
       xmlhttp=false; 
      } 
     } 
    } 
    return xmlhttp; 
} 
function getNenes(medi) { 
    var strURL ="secondpage.php?id="+medi; 
    var req = getXMLHTTP(); 
    if (req) { 
     req.onreadystatechange = function() { 
      if (req.status == 200) { 
       document.getElementById('copy').innerHTML=req.responseText; 
      } 
     } 
     req.open("GET", strURL, true); 
     req.send(null); 
    } 
} 

這是一個功能腳本,如果你想用它來做任何你想要的...
現在我secondpage.php這是我....有點...

<?php 
include ('connect.php'); 
// lets make a header 
header('Content-Type: text/html; charset=utf-8'); 

if (isset($_GET['id'])) { 
// lets make a query and all that is need 
// to fetch the information based on the ID # 
} else { 
// lets add a header 
header ("Location: you_are_not_allow.php"); 
} 
?> 
<!-- let put our HTML --> 
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script> <!-- Required --> 

<div id="copy"> 
<!-- everything that is here is what we want --> 
<!-- form input's, div's, span's ...etc --> 
<!-- I have a few foreach for input's array --> 
<!-- Anything else is useless for the main page --> 
</div> 
<!-- Now I need to put the info for each input that I might get --> 
<!-- for that I'm using array's --> 
<?php 
echo '<script type="text/javascript">'; 
foreach ($m as $n) { 
echo '$("#d_'.$c.'").children("option[value=\''.$d->ds.'\']").prop("selected",true);'; 
    $c++; 
} 
echo '</script>'; 
?> 

所以,直到這一點一切工作「很好」,事情是,當我從secondpage.php得到的結果我不想要<腳本>只有什麼是在第一個div與ID複製...

我不使用並導致main_page.php衝突的事物是:

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script> 

<?php 
    echo '<script type="text/javascript">'; 
    foreach ($m as $n) { 
    echo '$("#d_'.$c.'").children("option[value=\''.$d->ds.'\']").prop("selected",true);'; 
     $c++; 
    } 
    echo '</script>'; 
    ?> 

因此,如何能我只取什麼是我<div id = "copy">裏面?

+0

看吧:http://stackoverflow.com/questions/23076982/replace-the-div-content-with-ajax?rq=1 – uncoder

+0

實際的工作方式,以相同的是我有,它給了我從secondpage.php這不是我想要的整個HTML ...但謝謝。 – Tanker

+0

當您向其他頁面發出AJAX請求時,它始終返回整個頁面,這就是它的設計方式。你可以做的是發送PHP頁面的一些信息,並讓它使用這些信息來只回應你要找的東西。我們很多時候都使用這種技術來返回整個頁面太笨拙。 –

回答

0

您可以使用JQuery $("#copy")來獲取此標記的內部以選擇標記。 $("#copy").html()得到裏面有什麼