【问题标题】:Convert dropdown to selection boxes with color and trigger drop down action将下拉菜单转换为带有颜色的选择框并触发下拉操作
【发布时间】:2016-08-04 09:08:18
【问题描述】:

请帮助将此下拉列表转换为具有颜色的选择框,例如白框、黑框等 [不是复选框]。

所以该页面正在加载,而不是显示下拉我需要显示颜色选择框,请帮助。

我尝试了一些代码,但它只是部分工作。

<table class="variations" cellspacing="0">
  <tbody>
    <tr>
      <td class="label">
        <label for="pa_available-colors">Available Colors</label>
      </td>
      <td class="value">
        <select id="pa_available-colors" class="" name="attribute_pa_available-colors" data-attribute_name="attribute_pa_available-colors">
          <option value="" selected="selected">Choose an option</option>
          <option value="black" class="attached enabled" selected="selected">Black</option>
          <option value="white" class="attached enabled" selected="selected">White</option>
          <option value="red" class="attached enabled" selected="selected">Red</option>
        </select>
      </td>
    </tr>
  </tbody>
</table>

请帮忙。请看下图,我需要将下拉列表转换为这种类型的框

【问题讨论】:

  • 你的意思是你想要colorpicker
  • 带颜色的选择框
  • @Pugaz 朋友,非常感谢您的努力。现在请看我的问题。另外,请您根据我的html代码编写代码
  • 改用radio 怎么样? Jsfiddle

标签: javascript jquery html css


【解决方案1】:

使用下拉列表:

答案很简单:你不能。

让我解释一下原因。这个问题的答案也非常非常简单:MSDN on option tag。你可以在那里看到这段:

除了背景颜色和颜色之外,通过选项元素的样式对象应用的样式设置将被忽略。此外,直接应用于单个选项的样式设置会覆盖应用于整个选择元素的样式设置。

当然,您可以将下拉列表转换为something else(它甚至在链接问题中...不要那样懒惰),但它仍然是那么下拉列表呢?有关使用例如的更多信息,请参阅答案的下一部分。 radio 元素。

使用别的东西:

作为一种解决方法,您可以尝试使用例如radio 元素:

/* General stuff - radio button. */
input[type="radio"] {
  position: relative;
  width: 22px;
  height: 22px;
  vertical-align: bottom;
}
input[type="radio"]:checked:before {
  border: 1px solid #111;
}
/* The "background" - white background with gray border. */
input[type="radio"]:before {
  position: absolute;
  top: -2px;
  left: -2px;
  content: "";
  display: block;
  width: 22px;
  height: 22px;
  border: 1px solid #999;
  border-radius: 3px;
  background-color: #fff;
}
/* The "foreground" - color "square". */
input[type="radio"]:after {
  position: absolute;
  top: 1px;
  left: 1px;
  content: "";
  display: block;
  width: 18px;
  height: 18px;
  border-radius: 2px;
  box-shadow: inset 1px 1px 3px 1px rgba(0,0,0,0.12);
}
/* Colours */
input[type="radio"][value="red"]:after {
  background: #c44;
}
input[type="radio"][value="green"]:after {
  background: #4c4;
}
input[type="radio"][value="blue"]:after {
  background: #44c;
}
Colour:
<div class="list">
  <input type="radio" name="color" value="red" checked>
  <input type="radio" name="color" value="green">
  <input type="radio" name="color" value="blue">
</div>   

这里是隐藏(类似子菜单):https://jsfiddle.net/sts77L2h/

通过一些额外的工作,您应该能够使其更像下拉列表。或者,您可以对其进行试验以获得您可以想象的任何结果。

显然,您还可以尝试使用 radio 以外的其他元素,甚至查看(我在撰写本文时不知道)提供此类 form 元素的完整且有效的框架。

TD;DR:

来自this question 的转换器与我的 CSS 合并:JSFiddle。

根据您的需要调整此解决方案。

【讨论】:

    【解决方案2】:

    使用 HTML5 input 和 type="color",这里是示例:

    在选项列表中,您可以指定要查看的颜色列表。

     <table class="variations" cellspacing="0">
          <tbody>
            <tr>
              <td class="label">
                <label for="pa_available-colors">Available Colors</label>
              </td>
              <td class="value">
               <input type="color" id="pa_available-colors" name="attribute_pa_available-colors" data-attribute_name="attribute_pa_available-colors" list="colors">
     <datalist id="colors">
      <option>#ff0000</option>
      <option>#0000ff</option>
      <option>#00ff00</option>
      <option>#ffff00</option>
      <option>#00ffff</option>
     </datalist>
              </td>
            </tr>
          </tbody>
        </table>

    【讨论】:

    • 我想将下拉菜单更改为 radio 等框,但带有颜色。请使用我给定的html
    【解决方案3】:

    这是 CSS:

    select option[value="black"]{
       color:#fff;
        background: black;
    }
    
    select option[value="white"]{ 
    color:#000;
        background: white;
    }
    select option[value="red"]{
       color:#fff;
        background: red;
    }
    <table class="variations" cellspacing="0">
      <tbody>
        <tr>
          <td class="label">
            <label for="pa_available-colors">Available Colors</label>
          </td>
          <td class="value">
            <select id="pa_available-colors" class="" name="attribute_pa_available-colors" data-attribute_name="attribute_pa_available-colors">
              <option value="" selected="selected">Choose an option</option>
              <option value="black" class="attached enabled" selected="selected">Black</option>
              <option value="white" class="attached enabled" selected="selected">White</option>
              <option value="red" class="attached enabled" selected="selected">Red</option>
            </select>
          </td>
        </tr>
      </tbody>
    </table>

    但是,我建议您将selected="selected" 添加到仅value=""。

    【讨论】:

    • 我想将下拉菜单更改为 radio 之类的框,但带有颜色。请使用我给定的html
    • 在谷歌中搜索“单选按钮设计”,然后编辑您的单选按钮作为结果。
    【解决方案4】:

    更新

    使用您的 HTML。

    见FIDDLE

    原创

    您可以使用一些 JavaScript 将您的选择转换为收音机。然后使用一些 CSS 来处理生成的 HTML 的呈现。

    由此转化;

    <div class="value">
      <select id="pa_available-colors" class="" name="attribute_pa_available-colors" data-attribute_name="attribute_pa_available-colors">
        <option value="">Choose an option</option>
        <option value="black" class="attached enabled" selected="selected">Black</option>
        <option value="white" class="attached enabled">White</option>
        <option value="red" class="attached enabled">Red</option>
      </select>
    </div>
    

    到这个;

    <div class="value">
      <input type="radio" name="attribute_pa_available-colors" value="">
      <label for="attribute_pa_available-colors">Choose an option</label>
    
      <input type="radio" name="attribute_pa_available-colors" value="black" checked="checked">
      <label for="attribute_pa_available-colors">Black</label>
    
      <input type="radio" name="attribute_pa_available-colors" value="white">
      <label for="attribute_pa_available-colors">White</label>
    
      <input type="radio" name="attribute_pa_available-colors" value="red">
      <label for="attribute_pa_available-colors">Red</label>
    </div>
    

    在此处查看FIDDLE

    【讨论】:

    • 我想将下拉菜单更改为 radio 之类的框,但带有颜色。请使用我给定的html
    • 使用给定的 HTML,您选择了所有 3 个选项 - 这是故意的吗?鉴于您希望您的选择表现得像收音机,这似乎不正确。除非您尝试转换已经存在的 HTML。
    • 好吧,这就是我在最新的小提琴中提供的,除非我遗漏了什么?你想让我更新样式以反映 codecanyon 示例吗?
    • 是的。请问可以吗。
    • 查看更新后的FIDDLE 样式。
    【解决方案5】:

    我认为这段代码会帮助你做你想做的事情

    HTML

    <table class="variations" cellspacing="0">
      <tbody>
        <tr>
          <td class="label">
            <label for="pa_available-colors">Available Colors : </label>
          </td>
          <td class="value">
            <select id="pa_available-colors" class="" name="attribute_pa_available-colors" data-attribute_name="attribute_pa_available-colors">
              <option value="" selected="selected">Choose an option</option>
              <option value="black" class="attached enabled">Black</option>
              <option value="white" class="attached enabled">White</option>
              <option value="red" class="attached enabled">Red</option>
            </select>
          </td>
        </tr>
      </tbody>
    </table>
    

    CSS

    .selectbox>span {
      padding: 10px;
      border: 2px solid #fff;
      display: inline-block;
      vertical-align: middle;
    }
    
    .selectbox {
      border: 1px solid #dddddd;
      display: inline-block;
      cursor: pointer;
    }
    
    .selectbox.active {
      border: 1px solid #333;
    }
    

    jQuery

    $("#pa_available-colors option").each(function() {
      //get values of all option
      var val = $(this).val();
    
      //do magic create boxes like checkbox
      $("td.value").append('<div class="selectbox" data-color="' + val + '"><span style="background-color:' + val + '"></span></div>');
    
    });
    //remove empty selectbox
    $('.selectbox[data-color=""]').remove();
    
    //change select box on click
    $(".selectbox").click(function() {
      //remove selected from others
      $(".selectbox").removeClass("active");
      //do active to selected
      $(this).addClass("active");
      //get value
      var optVal = $(this).data("color");
    
      $("#pa_available-colors").val(optVal) 
    
    });
    
    //change select box on dropdown change
    $("#pa_available-colors").change(function(){
        var optVal = $(this).val();
      $(".selectbox").removeClass("active");
      $(".selectbox[data-color='"+optVal+"']").addClass("active");
    })
    

    这里是fiddle

    【讨论】:

    • 是的,这是我真正想要的。
    • 但在小提琴中你错过了这个 。请告诉当这也在代码中时会发生什么?
    • 实际上我的代码代表这个选择框选项如果你添加上面的选项你会看到空白选项
    • 在这种情况下,您需要过滤空白值或删除它们
    • 这是你需要的解决方案jsfiddle.net/12uax7cv/2删除空复选框
    猜你喜欢
    • 1970-01-01
    • 2010-11-23
    • 2021-11-01
    • 1970-01-01
    • 2016-03-06
    • 2014-06-22
    • 2011-06-16
    • 2021-09-01
    相关资源
    最近更新 更多