简体   繁体   English

多次使用Javascript函数

[英]Multiple usage of Javascript function

I got some code in JS which i use in my php script.( based on chartjs) 我在JS脚本中使用了JS中的一些代码。(基于chartjs)

$licznik=0;
    foreach ($all as $key => $row) {
                                      $imiona[$licznik]=$row['imie'];
                                      $nazwiska[$licznik]=$row['nazwisko'];
                                      $tak[$licznik]=$row['tak'];
                                      $nie[$licznik]=$row['nie'];
                                      $licznik++;
                                    }


    $wynik.='<div>
      <canvas id="'.$team.'">
      </canvas>
      </div>
      <br><hr>';

   $wynik.='<script>

  var barChartData = {
    labels : ["'.wypiszimiona($imiona,$nazwiska,$licznik).'], //radni
    datasets : [
      { //głosy na nie
        fillColor : "#BA0606",
      strokeColor : "#FF0808",
        highlightFill: "rgba(220,220,220,0.75)",
        highlightStroke: "rgba(220,220,220,1)",
        data : ['.wypisznie($nie,$licznik).']
      },
      { //głosy na tak
        fillColor : "#23BA06",
        strokeColor : "#30F70A",
        highlightFill : "rgba(151,187,205,0.75)",
        highlightStroke : "rgba(151,187,205,1)",
        data : ['.wypisztak($tak,$licznik).']
      }
    ]
  };
  window.onload = function(){
    var ctx = document.getElementById("'.$team.'").getContext("2d");
    window.myBar = new Chart(ctx).StackedBar(barChartData, {
      responsive : true
    });

      };
  </script>';

It render only last canvas, the other one are not rendered ( but JS code is right (in source). I have no idea what's wrong. I'm not good in JS:( 它只渲染最后一个画布,另一个不渲染(但是JS代码正确(在源代码中)。我不知道出了什么问题。我对JS不好:(

Based on what little information you provide, I assume you are copying and pasting that js code with different php variables. 根据您提供的少量信息,我假设您正在使用不同的php变量复制和粘贴该js代码。 Each time you do that, you are adding code that tells the client that 每次执行此操作时,您都会添加代码来告知客户端

window.onload = function(){
    var ctx = document.getElementById("'.$team.'").getContext("2d");
    window.myBar = new Chart(ctx).StackedBar(barChartData, {
      responsive : true
    });

if you do this more than once, you are overwriting window.onload, and only the last rewrite will happen, meaning only the last canvas will be drawn to. 如果多次执行此操作,则将覆盖window.onload,并且只会进行最后的重写,这意味着将仅绘制最后的画布。

You need to close (and open in some of them) the JS strings with double quotes. 您需要关闭(并打开其中的一些)带有双引号的JS字符串。 There are a few lines that lack them: 有几行缺少它们:

...labels : ["'.wypiszimiona($imiona,$nazwiska,$licznik).'"]...
...data : ["'.wypisznie($nie,$licznik).'"]...
...data : ["'.wypisztak($tak,$licznik).'"]...

I might have missed some. 我可能想念一些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM