2014-05-08 08:31
采纳率: 100%
浏览 53


Okay so I know this has been asked before and I've tried the solutions, but for some reason they don't work for me so I'm asking for some help. I haven't used JSON yet so maybe it's something silly but I have no clue...

Here's the code:


    #successful attempt to display array with json_encode in php
    echo json_encode($array);
<input id="show" type="button" onclick="showArray()" value="showArray">
function showArray(){
var array = <?php echo json_encode($array); ?>;

    //Failed attempt to display array in the div field show

    //Failed attempt to display the array with an alert.
    for(var i=0; i<2; i++){

So what do you guys think? Am I missing something? Is it possible that the array was successfully passed to javascript but for some reason won't show?




So I'm getting a series of arrays from a text file. I use these arrays as strings to display on the page and then convert them to float arrays. When I echo one of the float arrays such as $Z_Ausmass with:

echo json_encode($Z_Ausmass);

I get [25.39999961853,121.48651123047]. However, when I use the following to display the array through javascript:

function calc(){
var Z_Ausmass = <?php echo json_encode($Z_Ausmass); ?>;
    for(var o=0; o<Z_Ausmass.length; o++){
    var textnode = document.createTextNode(Z_Ausmass[o]);

it does not work. It's vital I get the float arrays in the script because the script needs to make calculations based on them and then display the calculations to the user.

图片转代码服务由CSDN问答提供 功能建议

好的,所以我知道之前已经问过这个问题并且我已经尝试了解决方案,但由于某种原因他们不知道 为我工作,所以我要求一些帮助。 我还没有使用过JSON所以也许它是愚蠢的但是我没有线索...... </ p>

这是代码:</ p>

 &lt  ;?php 
 $ array; 
 echo json_encode($ array); 
&lt; html&gt; 
&lt; input id =“show”  type =“button”onclick =“showArray()”value =“showArray”&gt; 
&lt; divShow&gt; 
&lt; / divShow&gt; 
&lt; script&gt; 
function showArray(){
var array =&lt;?php echo  json_encode($阵列);  ?&gt;; 
 //尝试显示数组时出现失败 alert。
 for(var i = 0; i&lt; 2; i ++){
 alert(array [i]); 
&lt; / script&gt; 
&lt; / html&gt; 
 <  / code> </ pre> 

那你觉得怎么样? 我错过了什么吗? 是否有可能将数组成功传递给javascript但由于某种原因不会显示?</ p>

谢谢,</ p>

-Alex </ p>

编辑:</ p>

所以我从文本文件中获取了一系列数组。 我使用这些数组作为字符串在页面上显示,然后将它们转换为浮点数组。 当我用$ Z_Ausmass回显其中一个浮点数组时:</ p>

  echo json_encode($ Z_Ausmass); 
 </ code> </ pre> 
 <  p>我得到[25.39999961853,121.48651123047]。 但是,当我使用以下内容通过javascript显示数组时:</ p> 
  function calc(){
var Z_Ausmass =&lt;?php echo json_encode($ Z_Ausmass);  ?&gt ;; 
 for(var o = 0; o&lt; Z_Ausmass.length; o ++){
 var textnode = document.createTextNode(Z_Ausmass [o]); 
 document.getElementById(“divCalc”)。appendChild(  textnode); 
 </ code> </ pre> 

它不起作用。 在脚本中获取浮点数组至关重要,因为脚本需要根据它们进行计算,然后将计算结果显示给用户。</ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题