【问题标题】:Get value when :checked from input type color获取值时:从输入类型颜色检查
【发布时间】:2023-03-15 19:08:01
【问题描述】:

我已经编写了这段代码,我想在每次点击输入时获得一个值。前两个输入正常工作,但 3 没有。我想让 3 按照这个原则工作:当我点击它时,会打开一个选择颜色的窗口,当我选择它时,我会在控制台中获得所选颜色的十六进制颜色。请帮我。 编辑:正如您在小提琴中看到的那样。我希望可以选择黑白并使用输入类型颜色选择颜色。我希望输入类型颜色像其他输入项一样获得一个框架。此外,您可以做些什么来确保当您选择一种颜色时,颜色渐变会变为您选择的颜色渐变?

var radio = document.querySelectorAll('input');
for(i=0;i<radio.length;i++){
  radio[i].addEventListener('click',function(){
    var color = document.querySelector('input:checked').value;
    console.log(color);
  });
}
input {
    position: absolute;
    width: 0;
    height: 0;
    opacity: 0;
}
label {
    display: block;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: 3px solid #fff;
    margin: 2px;
    box-shadow: inset 0 0px 2px 1px rgba(0,0,0,.1);
}
label:nth-child(2) {
    background-color: #000;
}
label:nth-child(4) {
    background-color: #fff;
}
label:nth-child(6) {
    background: conic-gradient(from 90deg,violet,indigo,blue,green,yellow,orange,red,violet);
}
input:checked + label {
    box-shadow: inset 0 0px 2px 1px rgba(0,0,0,.1),
                0 0 0 1px #000;
}
<input type="radio" name="color" id="color1" value="#000000" checked>
<label for="color1"></label>
<input type="radio" name="color" id="color2" value="#fffffff">
<label for="color2"></label>
<input type="color" name="color" id="color3">
<label for="color3"></label>

【问题讨论】:

    标签: javascript html input


    【解决方案1】:

    这可能有助于将其分开并在最终颜色处使用选择器颜色。

    如果您选择收音机,它也会将值设置为mastercolor

    var radio = document.querySelectorAll('input');
    var masterColor = document.getElementById('color3');
    for(i=0;i<radio.length;i++){
      radio[i].addEventListener('click',function(){
        var color = document.querySelector('input:checked').value;
        masterColor.value = color
      });
    }
    
    <input type="radio" name="color" id="color1" value="#000000" checked>
    <label for="color1">Black</label>
    <input type="radio" name="color" id="color2" value="#fffffff">
    <label for="color2">White</label>
    
    <input type="color" name="mastercolor" value="#000000" id="color3">
    <label for="color3">Pick</labe>
    

    为该用户提供最终颜色的主色

    【讨论】:

    • 非常感谢!如果我单击输入类型颜色以从其他输入中删除选中,您知道该怎么做吗?
    • 我要问另一个问题来更好地说明我的意思:D
    • 我建议使用另一个输入,而不是 Radio,它必须点击前导
    • 我编辑了这个问题,我想现在很清楚我想要实现的目标。
    【解决方案2】:

    您只需在 input 更改后获取它的值。 希望这个小sn-p有所帮助。

    [编辑]

    添加了问题的代码 sn-p。带有工作示例。 您也可以使用for 循环,但您需要在颜色选择器上使用change,而不是click 事件,因为它在click 发生后发生了变化。

    [/编辑]

    function color(option) {
    
      let colors = document.querySelectorAll("label");
      for (i = 0; i < colors.length; i++) {
        colors[i]
          .style
          .boxShadow = "";
      }
    
      let color;
      switch (option.type) {
      
        case "click":
          color = option.toElement.value;
          break;
        case "change":
          color = option.target.value;
          document
            .getElementById(option.target.id + "l")
            .style
            .background = color;
          document
            .getElementById(option.target.id + "l")
            .style
            .boxShadow = "inset 0 0px 2px 1px rgba(0,0,0,.1), 0 0 0 1px #000";
          let checked = document.querySelectorAll('input:checked');
          for (i = 0; i < checked.length; i++) {
            checked[i].checked = false;
          }
          break;
      }
      console.log(color);
    }
    input {
        position: absolute;
        width: 0;
        height: 0;
        opacity: 0;
    }
    label {
        display: block;
        width: 34px;
        height: 34px;
        border-radius: 50%;
        border: 3px solid #fff;
        margin: 2px;
        box-shadow: inset 0 0px 2px 1px rgba(0,0,0,.1);
    }
    label:nth-child(2) {
        background-color: #000;
    }
    label:nth-child(4) {
        background-color: #fff;
    }
    label:nth-child(6) {
        background: conic-gradient(from 90deg,violet,indigo,blue,green,yellow,orange,red,violet);
    }
    input:checked + label {
        box-shadow: inset 0 0px 2px 1px rgba(0,0,0,.1),
                    0 0 0 1px #000;
    }
    <input type="radio" name="color" id="color1" onclick="color(event)" value="#000000" checked>
    <label for="color1"></label>
    <input type="radio" name="color" id="color2" onclick="color(event)" value="#fffffff">
    <label for="color2"></label>
    <input type="color" name="color" id="color3" onchange="color(event)" value="#abcdef">
    <label for="color3" id="color3l"></label>

    【讨论】:

    • 好的,但是我编辑了这个问题,我想现在很清楚我想要实现的目标。
    • 工作很酷,但是如果选中,输入周围没有圆圈来选择颜色:/
    • 根据我提供的信息,您应该能够自己解决这个问题,但是因为我真的很喜欢这个小挑战,所以我更新了我的代码,取消选中并在颜色周围加上圆圈图形选择器。
    【解决方案3】:

    不应该这样吗:

    <input type="color" name="color" id="color3" value="#f90000">
    

    是:

    <input type="radio" name="color" id="color3" value="#f90000">
    

    【讨论】:

    • 但我想从输入类型颜色中选择颜色
    • 那么这个输入类型没有检查状态。您必须为此添加一个单独的事件侦听器,而无需 :checked,然后它应该可以工作
    • 我编辑了这个问题,我想现在很清楚我想要实现的目标。
    【解决方案4】:

    您的第三个输入是input type="color",而不是input type="radio"。我不认为输入类型“颜色”具有“已检查”状态。
    'color3'也没有价值,所以没有什么好挑的。

    <input type="radio" name="color" id="color1" value="#000000" checked>
    <label for="color1">Black</label>
    <input type="radio" name="color" id="color2" value="#fffffff">
    <label for="color2">White</label>
    <input type="radio" name="color" id="color3" value="#FFC0CB" >
    <label for="color3">Pink</label>
    

    【讨论】:

    • 我编辑了这个问题,我想现在很清楚我想要实现的目标。
    猜你喜欢
    • 2016-07-05
    • 2012-01-10
    • 2017-02-24
    • 2017-04-13
    • 2015-04-09
    • 2017-03-28
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多