所以我想显示一个不同的图像阵列,使用javascript给出单选按钮选择,但无法获得代码运行。使用jquery的解决方案也可以工作。基于单选按钮选择显示来自所选阵列的图像?
<script language="javascript"> var surffamous = new Array; surffamous[0] = "teahupoo.jpg" surffamous[1] = "blacks.jpg" surffamous[2] = "supertubes.jpg" surffamous[3] = "pipeline.jpg" var surfsocal = new Array; surfsocal[0] = "lajolla.jpg" surfsocal[1] = "oceanside.jpg" surfsocal[2] = "delmar.jpg" surfsocal[3] = "cardiff.jpg" var surfcentralcal = new Array; surfcentralcal[0] = "rincon.jpg" surfcentralcal[1] = "leocarillo.jpg" surfcentralcal[2] = "elcapitan.jpg" surfcentralcal[3] = "hollister.jpg"
<form style="color: #2D5059"> <p>Where would you like to explore?</p< <br> <br> <input type="radio" name="spot" id="socal" value="southern">Southern California <input type="radio" name="spot" id="central" value="central">Central California <input type="radio" name="spot" id="famous" value="famous">Famous Spots </form> <script language="javascript"> function check() { for(i=0; i<4; i++){ if(document.getElementById('socal').checked) { document.write('<img src="'+ surfsocal[i] +'"/> ' + '</a>'); } else if(document.getElementById('central').checked) { document.write('<img src="'+ surfcentralcal[i] +'"/> ' + '</a>'); } else if(document.getElementById('famous').checked) { document.write('<img src="'+ surffamous[i] +'"/> ' + '</a>'); } } </script>
回答
function check(spot) {
var whichArray;
if (spot.value == "southern") {
whichArray = surfsocal;
}
else if (spot.value == "central") {
whichArray = surfcentralcal;
}
else if (spot.value == "famous") {
whichArray = surffamous;
}
DisplayImages(whichArray);
}
function DisplayImages(images) {
for(i=0; i<images.length; i++) {
document.write('<img src="'+ images[i] +'"/> ');
}
}
然后加入onclicks的单选按钮
<input type="radio" name="spot" id="socal" value="southern" onclick="check(this);">Southern California
<input type="radio" name="spot" id="central" value="central" onclick="check(this);">Central California
<input type="radio" name="spot" id="famous" value="famous" onclick="check(this);">Famous Spots
没有测试,但应该让你开始
您的代码不会运行,因为它会抛出错误:您错过了}
令牌来'关闭'检查功能。
试试这个,我做了一些改变:
#images img {
margin: 1em;
width: 100px;
height: 100px;
}
<form style="color: #2D5059">
<p>Where would you like to explore?</p>
<input type="radio" name="spot" id="socal" value="southern">
<label for="socal">Southern California</label>
<input type="radio" name="spot" id="central" value="central">
<label for="central">Central California</label>
<input type="radio" name="spot" id="famous" value="famous">
<label for="famous">Famous Spots</label>
</form>
<br>
<div id="images"></div>
<script>
// Find the images div and all of the radio buttons
var images = document.getElementById('images'),
radioButtons = document.getElementsByName('spot');
// Use [] instead of new Array()
var surffamous = [];
surffamous[0] = "teahupoo.jpg"
surffamous[1] = "blacks.jpg"
surffamous[2] = "supertubes.jpg"
surffamous[3] = "pipeline.jpg"
var surfsocal = [];
surfsocal[0] = "lajolla.jpg"
surfsocal[1] = "oceanside.jpg"
surfsocal[2] = "delmar.jpg"
surfsocal[3] = "cardiff.jpg"
var surfcentralcal = [];
surfcentralcal[0] = "rincon.jpg"
surfcentralcal[1] = "leocarillo.jpg"
surfcentralcal[2] = "elcapitan.jpg"
surfcentralcal[3] = "hollister.jpg"
function check() {
var image,
fragment = document.createDocumentFragment();
for (var i = 0; i < 4; i++) {
// Don't use document.write
image = new Image();
// 'this' refers to the clicked radio button
if (this.id === 'socal') {
image.src = surfsocal[i];
} else if (this.id === 'central') {
image.src = surfcentralcal[i];
} else if (this.id === 'famous') {
image.src = surffamous[i];
}
// The fragment helps you minimize
// direct DOM insertions
fragment.appendChild(image);
}
images.innerHTML = '';
images.appendChild(fragment);
}
// Attach a listener to each button, using a loop
for(var i = radioButtons.length; i--;) {
radioButtons[i].onchange = check;
}
</script>
请确保您包括images
DIV后的脚本标签。 我们基本上是避免document.write
,将一个onchange
监听器附加到每个单选按钮,并创建一个图像,并在单击单选按钮(当它发生变化时)将其附加到div。
我也改变了一些东西,如new Array
字面声明[]
,并添加标签到您的单选按钮,以便当您单击文本时,就好像您单击按钮。
希望这对你有用。如果没有,请让我知道。祝你好运:)
嘿阿方索这个作品,但我想知道是否有办法填补图像或空间时,他们出现? – Taylor31
是的,当然,你只需要添加一些CSS。我已经更新了答案,看看顶部的CSS代码片段,我刚刚添加了一个“margin”属性。如果您愿意,您还可以控制图像的宽度和高度(如果希望它们保持其原始大小,请移除这些属性)。 –
- 1. 显示基于所选单选按钮的控件
- 2. 显示基于单选按钮选择不同的div
- 3. 基于值的单选按钮选择
- 4. 基于HTML/JavaScript中的单选按钮选择更改图像
- 5. 显示或隐藏基于多个单选按钮选择
- 6. 基于单选按钮选择显示/隐藏div
- 7. 显示面板基于单选按钮上选择(S)
- 8. 基于单选按钮选择显示内容
- 9. 基于单选按钮选择显示/隐藏CSS链接
- 10. 角JS:显示DIV基于单选按钮选择
- 11. 基于值选择单选按钮
- 12. 基于单选按钮选择
- 13. 使用jQuery来显示基于单选按钮选择的警报
- 14. 显示哪个单选按钮选择
- 15. 显示内容选择单选按钮
- 16. 如何选择特定的单选按钮来显示表单
- 17. 基于选择框选择的显示图像
- 18. 如何根据选择哪个单选按钮来交换显示的图像?
- 19. 基于下拉选择使用jquery选择单选按钮
- 20. jQuery代码来改变我怎样才能改变基于单选按钮选择可选择的下拉选项基于单选按钮选择
- 21. 单选按钮显示图像检查
- 22. 基于TYPO3 TCA表格中所选单选按钮的显示子菜单
- 23. 对于ng-repeat的所有行自动选择单选按钮
- 24. 基于单选按钮选择禁用按钮
- 25. 基于复选框选择的多个动态单选按钮
- 26. 动态显示关于选择的单选按钮
- 27. 基于单选按钮选择创建图块R Shiny
- 28. 单选按钮 - 显示基于与jQuery的选中按钮的DIV
- 29. 使用jQuery显示/隐藏基于单选按钮选择的DIV
- 30. 如何使用jQuery显示/隐藏基于单选按钮选择的div?
应该在这里花更多的时间格式化代码。还有什么问题/问题? –
对不起,第一次问这里的问题,所以通过我的初始格式。现在应该看起来更好!我只想显示一个基于单选按钮选择的数组(有三个选项)。 – Taylor31
当您单击收音机或按下按钮时是否要加载图像? –