Javascript - 如果选择下拉菜单,则更改容器中的图像
嘿伙计们。我试图用我认为简单的 JavaScript 来更改容器中的图像,但似乎无法弄清楚。 “cover-image”是图像所在的容器,“txtMontage”是下拉列表的ID。如果我按原样运行它,无论我选择什么,图像都是由第一个 if 语句设置的,然后下拉菜单会卡在上面,所以我无法选择其他任何内容。
关于如何解决这个问题有什么想法吗?谢谢。
<script type="text/javascript">
function showPreview() {
var image = document.getElementById("cover-image");
var dropd = document.getElementById("txtMontage");
if (dropd.value = "abrasives") {
var container= "img/abrasives.jpg";
image.src = container;
}
else if (dropd.value = "industrial") {
var container= "img/gen-industrial.jpg";
image.src = container;
}
}
</script>
Hey guys. I'm trying to change the image in a container with what I thought was simple javascript, but can't seem to figure it out. "cover-image" is the container where the image is, and "txtMontage" is the ID of the drop down list. If I run this as is, no mater what I select the image is set by the first if statement, and then the drop down gets stuck on it so I can't select anything else.
Any ideas on how to fix this? Thanks.
<script type="text/javascript">
function showPreview() {
var image = document.getElementById("cover-image");
var dropd = document.getElementById("txtMontage");
if (dropd.value = "abrasives") {
var container= "img/abrasives.jpg";
image.src = container;
}
else if (dropd.value = "industrial") {
var container= "img/gen-industrial.jpg";
image.src = container;
}
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在条件中指定
=
而不是==
:使用:
而不是:
You are specifying
=
instead of==
in your conditions:Use:
Instead of:
您需要将 if 更改为
In javascript = 设置对象的值,并使用 == 进行比较
You need to change your if to say
In javascript = sets the value of an object and == is used for comparison