Convert dropdowns to radio buttons w/o modifying HTML

前端 未结 2 1147
有刺的猬
有刺的猬 2020-12-29 15:42

So this is sort of an exceptional case situation - I have a plugin that I can\'t modify for contractual reasons. It displays a set of drop downs and I need it to display a s

2条回答
  •  盖世英雄少女心
    2020-12-29 16:12

    To encapsulate this into a declarative attribute, I extended @BrunoLM 's function to style all

    Result

    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 = $("
    "); select.after(container); container.append(select); select.find('option').each(function (optionIndex, optionElement) { var radioGroup = select.attr('id') + "Group"; var label = $("

提交回复
热议问题