在我的代码中,我使用 json 完成了一个 ajax 脚本并保存了您可以在下面看到的变量。之后,我尝试在我的if 语句中使用该变量,该变量是用 php 编码的。有可能这样做吗?
//Script variables
var $trstatus_1; //value = GROUP_3, GROUP_2, GROUP_1 or UNDEF
</script>
<?php
//Status
$id; //php variable
echo "<div id='images'>";
if($trstatus_1 == "GROUP_3"){
echo "<a id='".$id1."' class='tooltip1' href='#'><img src='ball_red_glossy.png' alt='' width='70' height='50' style='position: relative; left: 0px;' /><span>RED</span></a>";
}
else if($trstatus_1== "GROUP_2"){
echo "<a id='".$id1."' class='tooltip1' href='#'><img src='ball_orange_glossy.png' alt='' width='70' height='50' style='position: relative; left: 0px;' /><span>ORANGE</span></a>";
}
else if($trstatus_1 == "GROUP_1"){
echo "<a id='".$id1."' class='tooltip1' href='#'><img src='ball_yellow_glossy.png' alt='' width='70' height='50' style='position: relative; left: 0px;' /><span>YELLOW</span></a>";
}
else if($trstatus_1 == "UNDEF"){
echo "<a id='".$id1."' class='tooltip1' href='#'><img src='ball_blue_glossy.png' alt='' width='70' height='50' style='position: relative; left: 0px;' /><span>BLUE</span></a>";
}
echo "</div>";
?>
所以是的,唯一的问题是是否可以在我的 IF 语句中使用 $trstatus_1。如何 :)
BR
亚历克斯
更新
<script>
setInterval("yourAjaxCall()",10000);
function yourAjaxCall()
{
$.ajax(
{
type: "POST",
url: 'api.php', //the script to call to get data
data: {vtrstatus: $V_TR_STATUS}, //you can insert url argumnets here to pass to api.php
dataType: 'json', //data format
success: function(data) //on recieve of reply
{
var trstatus = data[0].group_class;
trstatus_1 = trstatus;
var trstatus_1;
//alert(trstatus_1);
}
});
};
</script>
<script>
//Script variables
var trstatus_1;
var color;
if( $trstatus_1 == 'GROUP_1' ){
color = 'red';
}else if ( $trstatus_1 == 'GROUP_2' ){
color = 'orange';
}else if ( $trstatus_1 == 'GROUP_3' ){
color = 'yellow';
}else if ( $trstatus_1 == 'UNDEF' ){
color = 'blue';
}
var el = "<a class='tooltip1' href='#'><img src='ball_" + color + "_glossy.png' alt='' width='70' height='50' style='position: relative; left: 0px;' /><span>" color.toUpperCase() "</span></a>";
$('#images').append(el);
</script>
<div id="images">
</div>
这个对吗?
BR