将下拉菜单转换为单选按钮,无需修改HTML

时间:2010-10-20 02:01:49

标签: javascript jquery html css

所以这是一种特殊情况 - 我有一个插件,我无法根据合同原因进行修改。它显示一组下拉菜单,我需要它来显示一组单选按钮。是否有js / jquery方法将下拉列表转换为单选按钮,而无需更改HTML。记住,我可以添加内容 - 我可以直接修改插件(以及HTML)。

我知道这是一个不好的方法,相信我,我不喜欢它比你做的更多。

可能通过检测下拉选项的值然后将它们重新格式化为单选按钮,隐藏原始下拉列表?

<form action="http://example.net/store/cart/" method="post" class="shopp product"> 
  <ul class="variations"> 
    <li> 
      <label for="options-1">Music Download</label> 
      <select name="products[117][options][]" class="category-catalog product117 options" id="options-1">
        <option value="">Select an option</option> 
        <option value="1">Full Album</option> 
        <option value="7">Theme</option> 
        <option value="8">Simian Segue  </option> 
        <option value="9">DK Island Swing</option> 
        <option value="10">Cranky's Theme</option> 
        <option value="11">Cave Dweller Concert</option> 
        <option value="12">Bonus Room Blitz</option> 
        <option value="13">Aquatic Ambiance</option> 
        <option value="14">Candy's Love Song</option> 
        <option value="15">Bad Boss Boogie</option> 
        <option value="16">Mine Cart Madness</option> 
        <option value="17">Life in the Mines</option> 
        <option value="18">Voices of the Temple </option> 
      </select>
    </li>       
  </ul> 

  <p> 
    <select name="products[117][quantity]" id="quantity-117">
      <option selected="selected" value="1">1</option><option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option><option value="5">5</option>
      <option value="6">6</option><option value="7">7</option>
      <option value="8">8</option><option value="9">9</option>
      <option value="10">10</option><option value="11">11</option><option value="12">12</option>
      <option value="13">13</option><option value="14">14</option>
      <option value="15">15</option><option value="20">20</option>
      <option value="25">25</option>
      <option value="30">30</option><option value="40">40</option>
      <option value="50">50</option><option value="75">75</option>
      <option value="100">100</option>
    </select>

    <input type="hidden" name="products[117][product]" value="117" />
    <input type="hidden" name="products[117][category]" value="catalog" />
    <input type="hidden" name="cart" value="add" />
    <input type="submit" name="addtocart"  value="Add to Cart" class="addtocart" />
  </p> 

</form>

2 个答案:

答案 0 :(得分:10)

隐藏下拉列表并将新的无线电元素放在表单之外,不需要发布,只需更新下拉列表值。

<强> Here is a code example on jsFiddle.

$("#d option").each(function(i, e) { // get the options
    $("<input type='radio' name='r' />") // create a radio element
        .attr("value", $(this).val()) // set the value
        .attr("checked", i == 0) // check the first by default
        .click(function () { // add event click which updates the dropdown
            $("#d").val($(this).val()); // update the dropdown if clicked
        })
        .appendTo("#r"); // append to some visible place
});

答案 1 :(得分:5)

为了将它封装到一个声明属性中,我将@BrunoLM的函数扩展为使用给定的CSS类将所有&lt; select&gt;的样式设置为单选按钮外观。

<强> jsFiddle

HTML

<select class="radioSelect" id="sizeOptions">
    <option value="S">S</option>
    <option value="M">M</option>
    <option value="L">L</option>
    <option value="XL">XL</option>
    <option value="2XL">2XL</option>
    <option value="3XL">3XL</option>
    <option value="4XL">4XL</option>
    <option value="5XL">5XL</option>
</select>

结果

enter image description here

CSS

/* http://jsfiddle.net/496c9/ */
.radioSelectContainer > select {
  display: none;
}

.radioSelectContainer > label {
  display: inline-block;
  margin: 0.3em 0.3em 0 0;
  background-color: #EFEFEF;
  border-radius: 4px;
  border: 1px solid #D0D0D0;
}

  .radioSelectContainer > label > span {
    padding: 0.2em;
    text-align: center;
    display: block;
    white-space: nowrap;
  }

  .radioSelectContainer > label > input {
    position: absolute;
    top: -20px;
  }

    .radioSelectContainer > label > input:checked + span {
      background-color: #404040;
      color: #F7F7F7;
    }

JS

// http://stackoverflow.com/questions/3975331/update-drop-down-selection-with-radio-button-selection-using-jquery
// http://jsfiddle.net/ChinmayeeG/TkGP8/
$(function () {

  $('.radioSelect').each(function (selectIndex, selectElement) {

    var select = $(selectElement);
    var container = $("<div class='radioSelectContainer' />");
    select.after(container);
    container.append(select);

    select.find('option').each(function (optionIndex, optionElement) {
      var radioGroup = select.attr('id') + "Group";
      var label = $("<label />");
      container.append(label);

      $("<input type='radio' name='" + radioGroup + "' />")
          .attr("value", $(this).val())
          //.click((function () { select.val($(this).val()); })) //radio updates select - see optional below
          .appendTo(label);

      $("<span>" + $(this).val() + "</span>").appendTo(label);
    });

    //http://stackoverflow.com/questions/4957207/how-to-check-uncheck-radio-button-on-click
    //optional - this logic handles unchecking when clicking on an already checked radio
    container.find(":radio + span").mousedown(
      function (e) {
        var $span = $(this);
        var $radio = $span.prev();
        if ($radio.is(':checked')) {
          var uncheck = function() {
            setTimeout(function () { $radio.prop('checked', false); }, 0);
          };
          var unbind = function() {
            $span.unbind('mouseup', up);
          };
          var up = function() {
            uncheck();
            unbind();
          };
          $span.bind('mouseup', up);
          $span.one('mouseout', unbind);
        } else {
          select.val($radio.val());
        }
      }
    );

    select.change((function () { //select updates radio
      $("input[name='" + select.attr('id') + "Group" + "'][value='" + this.value + "']").prop("checked", true);
    }));
  });
});