我对javaScript极为陌生,不知道如何创建一个具有图片描述的下拉框。在图片上选择后,将打开另一个窗口显示图片。有人可以帮忙吗?我究竟做错了什么?这个问题已经快两个星期了,我似乎无法弄清楚。谢谢。
function display_image(selectedImage) {
var selectionName =selectedImage.options[selectedImage.selectedIndex].text;
var selection = selectedImage.options[selectedImage.selectedIndex].value;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content=
"text/html; charset=utf-8">
<title>Lesson 5 Application Project</title>
</head>
<body>
<h3>CIW JavaScript Specialist</h3>
<hr />
<p>
Select an image:
</p>
<select name="images">
<option value="mountains.jpg">Mountains</option>
<option value="sunset1.jpg">Sunset</option>
<option value="trees.jpg">Trees</option>
</select>
</body>
</html>
答案 0 :(得分:0)
应该由选择框上的onChange事件处理。
如果要传递所选选项的值,则只需传递this.value
function display_image(selectedImage) {
console.log(selectedImage);
//imgURL-create imgURL based on the selected image
// here you can open new window and pass the image path
window.open(imgURL);
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN">
<html>
<head>
<meta http-equiv="Content-Type" content=
"text/html; charset=utf-8">
<title>Lesson 5 Application Project</title>
</head>
<body>
<h3>CIW JavaScript Specialist</h3>
<hr />
<p>
Select an image:
</p>
<select onchange="display_image(this.value)" name="images">
<option value="mountains.jpg">Mountains</option>
<option value="sunset1.jpg">Sunset</option>
<option value="trees.jpg">Trees</option>
</select>
</body>
</html>