2013-06-22 85 views
0

我試圖讓JSON數據的幾個部分一氣呵成像選擇部分標籤

<?php 
     $host = "localhost"; 
     $user = "root"; 
     $pass = ""; 
     $databaseName = "world"; 
     $tableName = "city"; 
     $con = mysql_connect($host,$user,$pass); 
     $dbs = mysql_select_db($databaseName, $con); 
     $result = mysql_query("SELECT * FROM $tableName limit 15"); //query 

     //fetch result 
    $jsonData = array(); 
    while ($array = mysql_fetch_row($result)) { 
     $jsonData[] = $array; 
    } 
    //first section 
    echo '<section id="stuff">'; 
    echo json_encode($jsonData); 
    echo '</section>'; 
    //section two .... 
    ?> 

我返回JSON包在HTML5的部分標籤內的數據

<section id="stuff">[["1","Kabul","AFG","Kabol","1780000"],["2","Qandahar","AFG","Qandahar","237500"],["3","Herat","AFG","Herat","186800"],["4","Mazar-e-Sharif","AFG","Balkh","127800"],["5","Amsterdam","NLD","Noord-Holland","731200"],["6","Rotterdam","NLD","Zuid-Holland","593321"],["7","Haag","NLD","Zuid-Holland","440900"],["8","Utrecht","NLD","Utrecht","234323"],["9","Eindhoven","NLD","Noord-Brabant","201843"],["10","Tilburg","NLD","Noord-Brabant","193238"],["11","Groningen","NLD","Groningen","172701"],["12","Breda","NLD","Noord-Brabant","160398"],["13","Apeldoorn","NLD","Gelderland","153491"],["14","Nijmegen","NLD","Gelderland","152463"],["15","Enschede","NLD","Overijssel","149544"]]</section> 

這是我的jquery

$.ajax({          
     url: 'getdata.php',       
     data: "",  
     dataType: 'html',     
     success: function(data)   
     { 
    //console.log(data); 
    var thedata = $(data).filter($("#stuff")); 
    console.log(thedata); 
     } 
    }); 

這是行不通的。我怎樣才能選擇節標籤內的數據?

+0

的爲什麼ü在'

目標吧'當U不想有它? – swapnesh

+0

我想在一個json調用中返回多個相關數據的數據集。 – Gandalf

回答

2

爲了得到內的元素使用的.html()

$(data).filter('#stuff').html(); 
0

找回JSON

$.ajax({          
     url: 'getdata.php',       
     data: "",  
     dataType: 'html',     
     success: function(data)   
     { 
     var thedata = $(data).filter('#stuff').html(); 
     var jobject = JSON.parse(thedata); 
     for(var i = 0; i < jobject.length; i++) { 
     var item = jobject[i]; 
     console.log(item[0] + ':' + item[1] + ':' + item[2] + ':' + item[3] + ':' + item[4]); 
     }} 
    });