我正在尝试创建一个允许用户提交一些数据的计算器(窗体),提交时将使用plotly.js绘制图形。现在,我只是试图获取一个示例图(与数据无关)以提交空表单时进行渲染。但是,该图会显示一秒钟,然后消失。似乎该图形在刷新页面半秒钟后才消失,然后消失了。如何防止这种情况发生?
<html>
<head>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<form>
<button type="submit" onclick="calculate()">Calculate</button><br>
</form>
<div id="myDiv"><!-- Plotly chart will be drawn inside this DIV --></div>
</body>
<script>
function calculate(){
var data = [
{
x: ['giraffes', 'orangutans', 'monkeys'],
y: [20, 14, 23],
type: 'bar'
}
];
Plotly.newPlot('myDiv', data);
};
</script>
</html>
答案 0 :(得分:1)
将按钮的类型从“提交”更改为“按钮”
function calculate(){
var data = [
{
x: ['giraffes', 'orangutans', 'monkeys'],
y: [20, 14, 23],
type: 'bar'
}
];
Plotly.newPlot('myDiv', data);
};
<html>
<head>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<form>
<button type="button" onclick="calculate()">Calculate</button><br>
</form>
<div id="myDiv"><!-- Plotly chart will be drawn inside this DIV --></div>
</body>
<script>
</script>
</html>