我正在编写一个表达每日图表的图表。我想通过选择标签更改图表。所以,我使用 javascript onchange。但是,它在第一次尝试时有效,但在第二次尝试时无效。https://jsfiddle.net/7eqn02yu/这是我的代码 JS 小提琴。这是代码。函数每次运行如何?<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.min.js"></script><button id="export-pdf">Export PDF</button><div id="map"></div><script src="./index.js"></script><script src="https://code.highcharts.com/highcharts.js"></script><select id="mySelect"><option value="0">mon</option><option value="1">tues</option><option value="2">wed</option><option value="3">thr</option></select><div id="container" style="height: 400px; width: 500px"></div><script type="text/javascript">const chart = Highcharts.chart('container', { plotOptions: { series: { point: { } } }, series: [{ data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4] }]});document.getElementById("mySelect").onchange = function () { myFunction() };function myFunction() { var x = document.getElementById("mySelect").value; if (x = 0) { const series = chart.series[0]; if (series.data.length) { chart.series[0].remove(); } chart.addSeries({ data: [100, 100, 100, 100, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5] }); } else if (x = 1) { const series = chart.series[0]; if (series.data.length) { chart.series[0].remove(); } chart.addSeries({ data: [200, 200, 200, 200, 29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5] }); } else if (x = 2) { const series = chart.series[0]; if (series.data.length) { chart.series[0].remove(); } chart.addSeries({ data: [200, 200, 200, 200] }); }}</script>
2 回答
喵喔喔
TA贡献1735条经验 获得超5个赞
代替if (x=0) use if (x===0)and 对于其余的 else if
如下:
if (x = 0){ // Use if (x === 0){
...
else if (x = 1){ // use else if (x===1)
const series = chart.series[0];
...
}else if (x = 2){// use else if (x===2)
添加回答
举报
0/150
提交
取消