简体   繁体   English

Highcharts oncahnge 事件它在第一次尝试时有效,但在第二次尝试时无效

[英]Highcharts oncahnge event It works on the first try but not on the second try

I'm coding a graph that expresses the day-by-day graph.我正在编写一个表示每日图表的图表。

I want to change the graph through select tag.我想通过选择标签更改图表。

So, i use javascript onchange.所以,我使用 javascript onchange。

But, It works on the first try but not on the second try.但是,它在第​​一次尝试时有效,但在第二次尝试时无效。

https://jsfiddle.net/7eqn02yu/ https://jsfiddle.net/7eqn02yu/

This is my code JS fiddle.这是我的代码 JS 小提琴。 And this is code.这是代码。 How does the function run every time?函数如何每次运行?

 <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>

The if were wrong formatted : if格式错误:

if (x = 0){

should be应该

if (x == 0){

Now it works JSFiddle现在它可以工作了JSFiddle

Instead of if (x=0) use if (x===0) and for the rest of the else if's代替if (x=0) use if (x===0)和 else if 的其余部分

as following:如下:

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)

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

相关问题 React setState 在第一次尝试时不起作用,但在第二次尝试时有效? - React setState not working on first try, but works on second? Firestore设置在第一次尝试时失败,但在第二次尝试时有效 - Firestore set fails on first try but works on second 第一次尝试时,链接点击事件未触发按钮点击事件(仅限IE)。 第二次单击即可工作。 - link click event not firing button click event on the first try (IE only). Works on second click. 快速路线仅在第二次尝试后有效 - Express route only works after the second try 单击事件适用于第三次或第四次尝试按钮 - Click event works on third or fourth try on button 尝试离线导出到 Highcharts - Try to offline exporting to Highcharts 如何在onCahnge事件中获得价值? - How to get value onCahnge event? React - 第一个文件上传到 Spring API 不起作用,再试一次,第二次尝试正常吗? - React - 1st file upload to Spring API doesn't work, try again and it works fine on second try? Ajax在第一次尝试时不发送textarea(CKEditor)的值,但是在第二次尝试时发送 - Ajax doesn't send the value of a textarea (CKEditor) on the first try, but it sends it on the second try HTML链接和Jquery Live仅在一次尝试时可以使用 - HTML Link and Jquery Live Only Works on First Try with one click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM