2017-04-24 124 views
0

我是PHP和JAVASCRIPT中的新成員。 我需要做一個小腳本,它將data.php中的數據與 $.getJSON("data.php", function(json)..並將其推送到HTML文件中的2個數組中,以重用它們。在現在我這樣做

[[47,48,48,48,50,51,48,46,47,45,48,47],[25,23,22,21,19,21,24,25,27,29,31,28]] 

,但它並沒有跑,我不知道該怎麼做:

data.php產生這樣。

<!DOCTYPE html> 
<html> 
<head> 
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script> 
<script> 
$(document).ready(function() { 
    $.getJSON("data.php", function(json) { 
      $row1[] = json[0]; 
      $row2[]= json[1];   

     }); 
    }); 

</script> 
</head> 
<body> 

<div></div> 

</body> 
</html> 

感謝所有的球員;)

+0

我覺得你正在用PHP和JS符號來混淆自己。 '$ array []'表示將數據附加到PHP數組中(http://php.net/manual/en/language.types.array.php#language.types.array.syntax.modifying),而不是JavaScript的。而'object [property]'是[訪問一個對象的屬性值](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_accessors)。但是,代碼中的$ row1 []'是一種無效的語法,因爲您沒有定義任何對象'$ row1',也沒有在'[]'中提供任何屬性。 – Mikey

回答

3

你的任務語法是錯誤的。您不能在Javascript中將變量的末尾加上[]

$(document).ready(function() { 
    $.getJSON("data.php", function(json) { 
     var row1 = json[0]; 
     var row2 = json[1]; 
     // put code that uses row1 and row2 here 
    }); 
});