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 ....
    ?>

我返回包装在 html5 部分标记中的 json

<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);
      } 
    });

这不起作用。如何仅选择部分标签内的数据?

4

2 回答 2

2

要获取元素的内部,请使用 .html()

$(data).filter('#stuff').html();
于 2013-06-22T07:30:39.050 回答
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] );
      }} 
    });
于 2013-06-22T07:52:17.780 回答