我试图在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);
}
});
这不起作用。如何只选择section标签内的数据?。
答案 0 :(得分:2)
要获取元素的内部,请使用.html()
$(data).filter('#stuff').html();
答案 1 :(得分: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] );
}}
});