将下拉列表转换为带有颜色的选择框并触发下拉操作

IT技术 javascript jquery html css
2021-02-25 09:47:06

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

在此处输入图片说明

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

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

<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>

请帮忙。请看下图,我需要将下拉转换为这种类型的框 在此处输入图片说明

5个回答

使用下拉列表:

答案很简单:你不能

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

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

当然,您可以将下拉列表转换为其他内容(甚至在链接问题中...不要那么懒惰),但它仍然是下拉列表吗?有关使用 egradio元素的更多信息,请参阅答案的下一部分

使用其他东西:

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

/* 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;博士:

来自这个问题的转换器与我的 CSS 合并:JSFiddle

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

将 HTML5input与 一起使用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
2021-04-25 09:47:06

这是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="".

在谷歌中搜索“单选按钮设计”,然后编辑您的单选按钮作为结果。
2021-04-25 09:47:06
我想将下拉列表更改为 Radio 之类的框,但带有颜色。请使用我给定的 html
2021-04-29 09:47:06

更新

使用您的 HTML。

小提琴

原来的

您可以使用一些 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,样式到位。
2021-04-24 09:47:06
是的 。你能做吗。
2021-04-27 09:47:06
我想将下拉列表更改为 Radio 之类的框,但带有颜色。请使用我给定的 html
2021-04-30 09:47:06
好的,这就是我在最新的小提琴中提供的内容,除非我遗漏了什么?您是否希望我更新样式以反映 codecanyon 示例?
2021-05-16 09:47:06
使用给定的 HTML,您选择了所有 3 个选项 - 这是故意的吗?鉴于您希望您的选择表现得像收音机,这似乎不对。除非您尝试转换已经存在的 HTML。
2021-05-18 09:47:06

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

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;
}

查询

$("#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");
})

这是小提琴

是的,这就是我真正想要的。
2021-04-22 09:47:06
在这种情况下,您需要过滤空白值或删除它们
2021-04-24 09:47:06
实际上,如果您添加上述选项,我的代码代表此选择框​​选项工作,您将看到空白选项
2021-05-03 09:47:06
但是在小提琴中你错过了这个 <option value="" selected="selected">Choose an option</option> 。请告诉当这也在代码中时会发生什么?
2021-05-14 09:47:06
这是你需要的解决方案jsfiddle.net/12uax7cv/2删除空复选框
2021-05-17 09:47:06