Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用JS对从JSON推送的值进行舍入_Javascript_Jquery_Json_Rounding - Fatal编程技术网

Javascript 使用JS对从JSON推送的值进行舍入

Javascript 使用JS对从JSON推送的值进行舍入,javascript,jquery,json,rounding,Javascript,Jquery,Json,Rounding,我是JS的新手,我想不出应该如何使用Json结构对来自外部文件IOCounter.html的值(Counter1和Counter2)进行取整 我试图将math.round()放在代码中的不同位置,这对我来说似乎是合乎逻辑的,但没有任何效果 我是否必须定义Var,将其四舍五入并将结果写入标签 谢谢你的帮助 <script src="jquery-2.1.1.min.js"></script> </head> <body> <label

我是JS的新手,我想不出应该如何使用Json结构对来自外部文件IOCounter.html的值(Counter1和Counter2)进行取整

我试图将math.round()放在代码中的不同位置,这对我来说似乎是合乎逻辑的,但没有任何效果

我是否必须定义Var,将其四舍五入并将结果写入标签

谢谢你的帮助

 <script src="jquery-2.1.1.min.js"></script>
 </head>
 <body>
  <label id="counter1">0</label>
  <label id="counter2">0</label>
 </body>

 <script type="text/javascript">
    $(document).ready(function(){
        $.ajaxSetup({ cache: false });
        setInterval(function() {
            $.getJSON("IOCounter.htm", function(result) {
                $('#counter1').text(result["counter1"]);
                $('#counter2').text(result["counter2"]);

            });
        },50);
    });

    </script>

0
0
$(文档).ready(函数(){
$.ajaxSetup({cache:false});
setInterval(函数(){
$.getJSON(“IOCounter.htm”,函数(结果){
$('#counter1')。文本(结果[“counter1]”);
$('#counter2')。文本(结果[“counter2]”);
});
},50);
});

您不必定义变量本身。你可以这样做:

$('#counter1').text(Math.round(result["counter1"]));

您确定
IOCounter.htm
包含JSON而不是HTML吗?然后将其重命名为
IOCounter.json
。谢谢您的评论。我正在PLC的Web服务器中使用代码。不太确定是否允许使用json扩展。无论如何现在它起作用了。在本地使用htm和json进行了尝试。谢谢。它工作得很好。我想我以前也试过这个。。。也许我又犯了一个错误。再次感谢。周末愉快!你好。我使用:$('#counter1').text(Math.round(result[“counter1”]*100)/100);获取逗号后面的两个数字。但是如果逗号后面没有数字-这个方法不打印XY.00-只打印XY,所以我尝试了:$('#counter1').text(result[“counter1”].toFixed(2));现在这正是我想要的。谢谢你的帮助!很高兴听到你明白了:)祝你周末愉快