【发布时间】:2020-05-30 04:21:29
【问题描述】:
- 如何在 Bootstrap 4 中使用 jQuery Knob Chart 显示从某个 URL 获取的值。我得到了“NaN”值。 (请参考下面的代码)
- 如果数据为空,如何显示“-”短划线符号。 (请参考下面的代码)
HTML
<input type="text" class="knob" id="avg_temperature" data-width="120" data-height="120" data-thickness="0.40" data-fgColor="#000000" readonly>
JS
$.ajax({
url : XXX,
type : 'POST',
dataType : 'json',
cache: false,
async: false,
dataSrc : 'data',
contentType: false,
processData: true,
success: function(response){
if (response.status == "Success"){
if (response.data[0]["avg_temperature"] == null){
response.data[0]["avg_temperature"] = "-";
$("#avg_temperature").text("-");
}
$("#avg_temperature").text(response.data[0]["avg_temperature"]);
var colors = ['#11E117', '#FFC300', '#C00']
//knob chart for temperature
$('#avg_temperature').knob();
$('#avg_temperature').attr('data-fgColor', '#11E117');
$({animatedVal: 0}).animate({animatedVal: response},{
duration: 3000,
easing: "swing",
async: false,
step: function() {
var val = Math.ceil(this.animatedVal);
$("#avg_temperature").trigger('configure', {
'fgColor': colors[(val < 40) ? 0 : (val < 70) ? 1 : 2]
}).val(val).trigger("change");
var newVal = val + String.fromCharCode(176) + 'C'; $('#avg_temperature').val(newVal);
}
});
}
},
});
【问题讨论】:
-
这似乎是孤立的:jsfiddle.net/RoryMcCrossan/qzfw36ct/1。您能否将问题的工作示例添加到问题中,以便我们创建 MCVE。请注意,可以删除 AJAX 部分,因为它与问题无关。
-
我已经在那个链接上提供了代码。用于 HTML 和 JS。如果这还不够,请告诉我。
-
response的值是多少? -
我创建了一个变量
$("#avg_temperature").val = "90";作为值。 -
不,我问你
$.ajax调用中response参数的值是多少。您在animate()方法中使用它。我假设它是一个对象,而不是一个整数,因此你的错误。从上下文看来您应该使用...animate({ animatedVal: parseInt(response.data[0]["avg_temperature"], 10) })
标签: jquery html ajax bootstrap-4 jquery-knob