2013-02-12 34 views
-1

我真的不知道该怎么称呼它。下面是我的文件从data.php获取结果,然后将其转换为javascript字符串

data.php =>计算一些随机数 jscript.js =>此回声从data.php结果一个DIV ID

但我想做一些部分得到来自data.php的值,然后将它放到 div的样式中。

$.get('jQuery/data.php?Con&userHP&perc' , { userID: xUserID } , 
    function(output) { 
     $('#userHP').html(output).show(); 
     var o = document.getElementById("userHealthbar"); 
     o.style.width= result_here ; 
    } 
); 

而不是把结果放在div/div里面我想把它放到div的样式部分。

P.S:我希望data.php的结果是javascript中的一个变量。

但我似乎无法使其工作。 我希望我的解释清楚。

+1

如果您需要从你的php脚本返回多个东西到你的javascript,你应该输出json并在js端处理它。 – jeroen 2013-02-12 12:45:28

回答

0

如果你想从PHP返回多个事情的JavaScript,你应该使用JSON。

因此,如果您想要在PHP返回2倍的值,你可以这样做:

$html = "your html string"; 
$value = 4; // the value you want to return 

$array_to_return = array("html" => $html, "value" => $value); 

echo json_encode($array_to_return); // the only thing you echo out in your php! 

然后,在JavaScript中,你可以使用$.getJSON代替$.get

$.getJSON('jQuery/data.php?Con&userHP&perc' , { userID: xUserID } , 
    function(output) { 
     $('#userHP').html(output.html).show(); 
     var o = document.getElementById("userHealthbar"); 
     o.style.width= output.value ; 
    } 
); 
1

您应该使用jQuery选择器而不是document.getelementById。

常用方式:

jQuery('#userHealthbar').attr('style', whateverYouGotFromPHP); 

宁在这种情况下:

jQuery('#userHealthbar').width(whateverYouGotFromPHP); 

D.

+0

所以我只是把'代码jQuery('#userHealthbar')。宽度(data.php);' 导致我的问题是如何从data.php中获取结果作为javascript中的变量使用。抱歉,我对JavaScript真的很陌生,只是试图用挫败感来理解它。 – user2034683 2013-02-12 12:49:20

相关问题