[英]Refresh Image Based on User Input in HTML Form
I have a series of visualizations that are loaded via JSON files. 我有一系列通过JSON文件加载的可视化文件。 I've created a form in HTML that allows the user to select the image they want to see:
我创建了一个HTML表单,该表单允许用户选择他们想要查看的图像:
<form action="#">
<fieldset>
<label for="selector"><b>Select a visualization</b></label>
<select name="selector" id="selector">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
</select>
</fieldset>
</form>
The visualization is loaded with JavaScript as such: 可视化文件将这样加载JavaScript:
var s = document.getElementsByName('selector')[0];
var text = s.options[s.selectedIndex].value;
var imageURL = "viz/" + text + ".json";
var spec = imageURL;
I've got the site set up so that the visualizations will change if I manually refresh the page, but how do I get the visualization to automatically refresh based on the user changing their selection in the form? 我已经设置好网站,以便在手动刷新页面时可视化效果会发生变化,但是如何根据用户更改表单中的选择,如何使可视化效果自动刷新?
Put your visualization load logic to onchange event of #selector: 将可视化加载逻辑放入#selector的onchange事件中:
<select name="selector" id="selector" onchange="changeVisualization()">
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
</select>
function changeVisualization(){
var s = document.getElementsByName('selector')[0];
var text = s.options[s.selectedIndex].value;
var imageURL = "viz/" + text + ".json";
var spec = imageURL;
}
you can use onchange on javascript. 您可以在JavaScript上使用onchange。 it will automatically appear without refreshing the page
它会自动显示而无需刷新页面
<input type='file' name='file' accept="image/*" onchange="loadFile(event)" value = "logo1.png" />
<center>
<img src="logo1.png" id="output" width="310" align = "center">
</center>
</div>
Javascript: 使用Javascript:
<script>
var loadFile = function(event) {
var reader = new FileReader();
reader.onload = function(){
var output = document.getElementById('output');
output.src = reader.result;
};
reader.readAsDataURL(event.target.files[0]);
};
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.