【问题标题】:Why isn't my onclick event listener on my radio buttons not working?为什么我的单选按钮上的 onclick 事件侦听器不起作用?
【发布时间】:2020-08-21 16:13:03
【问题描述】:

<form id="color-options" name="FormB">
       <input type="radio" name="choice" value="blue" onclick="updateColor()">
       <label for="blue">Blue</label>
       <input type="radio" name="choice" value="red" onclick="updateColor()" >
       <label for="red">Red</label>
       <input type="radio" name="choice" value="white" onclick="updateColor()" >
       <label for="white">White</label>
       <input type="radio" name="choice" value="user" onclick="updateColor()">
       <label for="color-picker">Color Picker</label>
       <input type="color" id="color-picker"> 
     </form>
      <div id="Sketch">

        <div id="tile">

        </div>
      </div>

    let tile = document.getElementById("tile")
    let radio = document.forms[1] // //forms[1] is accessing the 2nd form because u have a form before it.
    function updateColor() {
        for (let i = 0; i < radio.choice.length; i++) {   //document.forms returns a collection of everything that has a form tag
            if (radio.choice[i].checked ) {  //form.choice = returns an array of the radio buttons, incrementing i to traverse along the array
                //checked returns a boolean if the button is checked or not; ask if the button is clicked or not and if it is follow this new loop
               for (let i = 0; i < radio.choice[i].length; i++) { //this new loop iterates and asks if the value is a certain color, and if it is change the background color
                    if (radio.choice[i].value === "blue") {
                            tile.style.backgroundColor= "blue";
                    } else if (radio.choice[i].value === "red") {
                            tile.style.backgroundColor = 'red';
                            } else if (radio.choice[i].value === "white") {
                                tile.style.backgroundColor = 'white';
                            } else if (radio.choice[i].value === "user") {
                                tile.style.backgroundColor = 'green';
                            }
                        }
                    } 
                } 
            }

所以我试图让磁贴在悬停时根据选择的选项改变颜色。 (对于现在的示例,为了简单起见,它只是设置为更改背景颜色,尽管我还没有想出在悬停时这样做)。所以我试图做的是遍历表单组以查看是否检查了任何内容,然后是否移动到嵌套循环,询问该值是否为某种颜色。我觉得我的逻辑是正确的,但没有任何反应,我不确定我还能做什么。但显然有一个错误我只是没有抓住。

https://jsfiddle.net/Jbautista1056/0gxf2Lpq/1/

【问题讨论】:

    标签: javascript html dom radio-button dom-events


    【解决方案1】:

    您还可以通过 document.querySelector('#color-options &gt; input:checked') 使用 CSS 选择器来获取“选中”单选按钮。然后,您可以通过在末尾添加.value 来获取选中的单选按钮的值。

    总的来说; document.querySelector('#color-options &gt; input:checked').value。不需要循环,不需要参数。

    【讨论】:

    • 哇这个好消息!我不敢相信我经历了所有艰苦的工作只是为了环环相扣,哈哈
    • 始终如一。 :p
    • document.querySelector('#color-options > input:checked') 是否给你一个布尔返回值?
    • 不怕。它查看 DOM 并尝试找到匹配的节点。如果找不到则返回null,如果可以则返回该元素。
    • 我尝试将此代码实现到我的文件中,但颜色仍然没有改变。我还将 html 文件中的 onclick="" 更改为新函数。这就是我所做的。 pastebin.com/CkKR0zyJ
    【解决方案2】:

    您无需遍历表单即可检查检查了哪个元素。使用updateColor(this) 就足够了:

    function updateColor(element) {
      const selectedColor = element.value !== 'user' ? element.value : 'green';
      const title = document.getElementById("tile");
      tile.style.backgroundColor = selectedColor;
    }
    #tile {
      width: 100px;
      height: 100px;
      border: 1px solid #000;
    }
    <input type="radio" name="choice" value="blue" onclick="updateColor(this)">
    <label for="blue">Blue</label>
    <input type="radio" name="choice" value="red" onclick="updateColor(this)" >
    <label for="red">Red</label>
    <input type="radio" name="choice" value="white" onclick="updateColor(this)" >
    <label for="white">White</label>
    <input type="radio" name="choice" value="user" onclick="updateColor(this)">
    <label for="white">User</label>
    <br /><br />
    <div id="tile"></div>

    【讨论】:

    • 我喜欢这个解决方案,但我对 updateColor(this) 和参数元素如何与此联系感到困惑。 element 参数的值是从哪里得到的?
    • @JosemariBautista this 代表 HTML DOM 元素。例如,当您单击第一个单选(具有蓝色值的单选)时,整个元素将传递给 updateColor() 并且this 是该单选元素在 HTML DOM 中的表示。如果您使用 console.log(this),您将可以访问整个元素及其属性,例如 element.id、element.name 等。
    猜你喜欢
    • 1970-01-01
    • 2014-10-30
    • 2020-08-17
    • 2010-09-07
    • 2014-05-07
    • 2017-07-18
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多