【发布时间】:2014-11-20 01:14:51
【问题描述】:
我是 AJAX/jQuery 的新手,正在尝试新的东西。
here is my fiddle for better understanding每个方块都是DIVS。
<div class="desk_box_ver" id="desk_B20" data-rel="85" style="left:20px;top:1165px;">B20</div>
内部的数字是通过 AJAX 调用检索的,该调用通过执行 PHP 脚本获取它 一个查询,它将以“1300”替换“B20”作为示例。
问题:
如何根据显示的数字生成“热图”。 示例:假设数字范围是从 100(最低)到 1800(最高)。 根据数字范围,必须显示背景颜色 绿色、黄色、橙色和红色。
我在stackoverflow上发现的一个类似问题是this one
AJAX:我如何在 DIV 中显示数字
<script type="text/javascript">
$(document).ready(function() {
$('#aht').click(function(){
$.ajax({
type:"GET",
url : "show_aht.php",
data:{ } , // do I need to pass data if im GET ting?
dataType: 'json',
success : function(data){
//going through all DIVs only once with this loop
for(var i = 0; i < data.length; i++) { // loop over results
var divForResult = $('#desk_' + data[i]['station']); // look for div for this object
if(divForResult.length) { // if a div was found
这是我输出数字的地方
divForResult.html(data[i]['aht_value']); // set inner HTML with AHT value
}//end if
}//end for
}//end success
});//end ajax
});//end click
});//end rdy
</script>
正在从下面的数组中检索到的 show_aht.php 数字
$result = array();
foreach ($memo as $username => $memodata) {
if (in_array($username, array_keys($user))) {
// Match username against the keys of $user (the usernames)
$userdata = $user[$username];
//if AHT is null give N/A as value
if (is_null($memodata['aht_value'])) {
$result[] = array( 'username' => $userdata['username'],
'aht_value' => 'NA',
'station' => $userdata['station']
);
}//end inner if
//else give the actual value of AHT without the decimals
else {
$result[] = array( 'username' => $userdata['username'],
'aht_value' => substr($memodata['aht_value'],0,-3),
'station' => $userdata['station']
);
}//end else
}//end outer if
}//end for
echo json_encode($result);
【问题讨论】:
-
你的小提琴好像有问题
-
@SimonH 有什么问题?
标签: javascript php jquery css ajax