根据选择显示图像的 Javascript 函数不起作用

标签 javascript html

我正在尝试根据用户选择的颜色显示绿色、黄色或红色的圆点图像。我的代码基于我在 this question 上找到的东西.

由于某种原因,该功能无法正常工作。目前,当用户进行选择时没有任何反应。

var statusCode = document.getElementById('statusCode');
var greenDot = document.getElementById('greenDot');
var yellowDot = document.getElementById('yellowDot');
var redDot = document.getElementById('redDot');

function myfunction() {
  if (statusCode.value == 'Green') {
    greenDot.style.display = 'circle';
    yellowDot.style.display = 'none';
    redDot.style.display = 'none';
  } 
  else if (statusCode.value == 'Yellow') {
    greenDot.style.display = 'none';
    yellowDot.style.display = 'circle';
    redDot.style.display = 'none';
  } 
  else if (statusCode.value == 'Red') {
    greenDot.style.display = 'none';
    yellowDot.style.display = 'none';
    redDot.style.display = 'circle';
  }
}
<select id="statusCode" onload="myFunction()">
  <option value="Green">Green</option>
  <option value="Yellow">Yellow</option>
  <option value="Red">Red</option>
</select>

<p>
  <img src="http://clipart-library.com/image_gallery/156788.png" style="display: none;" id="greenDot">
  <img src="http://www.clker.com/cliparts/o/b/y/x/Z/c/yellow-dot-md.png" style="display: none;" id="yellowDot">
  <img src="http://www.clker.com/cliparts/s/o/v/c/T/Y/red-dot-md.png" style="display:none;" id="redDot">
</p>

最佳答案

我稍微修改一下你的代码:

  • 更改读取选择值的方式(在 let value =.... 行)
  • 更改 html <select ... onload...oninput
  • 改变 js myfunctionmyFunction (F 大写)
  • js 的变化 style.display = 'circle''block'
  • 调用myFunction()在脚本底部初始化第一张图片
  • 更改<img src=http://...https:// (但可能只有在 SO 中才需要)

var statusCode = document.getElementById('statusCode');
var greenDot = document.getElementById('greenDot');
var yellowDot = document.getElementById('yellowDot');
var redDot = document.getElementById('redDot');

function myFunction() {

    const value = statusCode.options[statusCode.selectedIndex].value

    if(value == "Green") {        
        greenDot.style.display = 'block';
        yellowDot.style.display = 'none';
        redDot.style.display = 'none';
    }
    else if(value == "Yellow"){
        greenDot.style.display = 'none';
        yellowDot.style.display = 'block';
        redDot.style.display = 'none';
    }
    else if(value == "Red"){
        greenDot.style.display = 'none';
        yellowDot.style.display = 'none';
        redDot.style.display = 'block';
    }
}

myFunction();
<select id="statusCode" onchange="myFunction()"> 
  <option value="Green">Green</option>
  <option value="Yellow">Yellow</option>
  <option value="Red">Red</option>
</select>

<p>    
  <img src="https://clipart-library.com/image_gallery/156788.png" style="display: none;" id="greenDot"/>
  <img src="https://www.clker.com/cliparts/o/b/y/x/Z/c/yellow-dot-md.png" style="display: none;" id="yellowDot"/>
  <img src="https://www.clker.com/cliparts/s/o/v/c/T/Y/red-dot-md.png" style="display:none;" id="redDot"/>
</p>

关于根据选择显示图像的 Javascript 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54677583/

相关文章:

html - 使选择文件按钮看起来像一个 div

javascript - ReactJS 循环对象不返回值

javascript - 使用 Math.round 后在 JavaScript 中为 -0?什么是-0?

javascript - 如何在 javascript/jquery 中单击文本时启用选项卡及其内容?

javascript - 左对齐时居中内容

html - 选择选择选项时如何停止表单字段跳转?

javascript - 无法使用附加到 "this"的任何 JQuery 方法

javascript - 服务 js 库 : better performance from google code or using asset packager?

javascript - 打开 d3 树布局以显示用户通过在搜索框中键入 request 搜索的节点

HTML/CSS 表格打乱了布局