我遇到了以下问题:我写了一个搜索函数,结果被保存到数组中。当我使用jquery form plugin处理该函数的响应时,我创建了一个额外的数组,其中填充了搜索创建的所有数组。然后,我想将该多数组解析为我的jQuery脚本作为JSON对象。到目前为止一切顺利,但我如何让多阵列可以访问脚本? (像multiarray.array1.property)
到目前为止,这是我的代码:
<!DOCTYPE html> <html> <body> <div class="edit"> <h2>Editieren</h2> <form id="suchen" method="post"><input type="text" id="search" name="id"> <input type="submit" value="Senden"></form> </div> </html>
$('#suchen').ajaxForm({
url: "./php/search.php",
dataType: 'json',
success: function(data){
alert(data[0]) ;
},
clearForm: true
}) ;
提前谢谢
编辑:
JSON示例:
{
"id": "33",
"firma": "sls",
"strasse": "Industriegebiet Siebend",
"plz": "66663",
"ort": "Merzig",
"vorname": "",
"nachname": "Ruf",
"email": "ronny.ruf@web.de ",
"bemerkung": "",
"partner": "",
"kinder": "1",
"nation": "D",
"betreuer": "Adam",
"anrede": "Herr"
}
答案 0 :(得分:2)
在你的PHP
while($row = mysql_fetch_array($result)){
$article = array (
"id"=>$row['id'],
"firma"=>$row['firma'],
"strasse"=>$row['strasse'],
"plz"=>$row['plz'],
"ort"=>$row['ort'],
"vorname"=>$row['vorname'],
"nachname"=>$row['nachname'],
"email"=>$row['email'],
"bemerkung"=>$row['bemerkung'],
"partner"=>$row['partner'],
"kinder"=>$row['kinder'],
"nation"=>$row['nation'],
"betreuer"=>$row['betreuer'],
"anrede"=>$row['anrede'],
) ;
$hits[] = $article;
}
echo json_encode($hits) ;
在你的jquery ......
$('#suchen').ajaxForm({
url: "./php/search.php",
dataType: 'json',
success: function(data){
$.each(data, function(i, val){
console.log(val); /*alert don't work on arrays*/
alert(val.firma); /*you can alert some array key for example*/
});
},
clearForm: true
}) ;
我用console.log替换alert,因为val将是数组... 要访问它的任何键只需写val.keyname ..
as .. alert(val.strasse);
答案 1 :(得分:0)
parseJSON
可以帮助您
var data = jQuery.parseJSON('{"name":"John"}');
alert( data.name );