从data.php中获取结果,然后将其转换为javascript字符串



我真的不知道该怎么称呼它。这是我的文件

data.php=>计算一些随机数jscript.js=>这会将data.php的结果回声到div id

但在某些方面,我想做的是从data.php中获取值,然后将其放置到的风格

    $.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的样式部分

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

但我似乎无法让它发挥作用。我希望我的解释清楚。

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

常用方式:

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

在这种情况下更好:

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

D。

如果你想从php到javascript返回多个东西,你应该使用json。

因此,如果您有两个想要在php中返回的值,您可以执行以下操作:

$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 ;
    }
);

最新更新