How can i change select tag option into button group?

早过忘川 提交于 2019-12-13 09:46:44

问题


<div class="form-group">
         <label>Select Duration</label>
           <select name="plan_duration" id="plan_duration" class="selectpicker show-tick form-control" data-live-search="true" style="width:100%;">

            </select>
 </div>

Related Scripts

$("#plan_name").change(function()
{
       $.getJSON("getduration.php?city="+ $(this).val(), success = function(data)
       {
       var options = "";
       for(var i = (0); i < data.length; i++)
           {
             options += "<option>" + data[i] + "</option>";

           }
       $("#plan_duration").html("");
       $("#plan_duration").append('<option>-Select Duration-</option>');
       $("#plan_duration").append(options);
       $("#plan_duration").selectpicker('refresh');
       console.log(options);    
       });
});

回答1:


Button groups in bootstrap are a way to display buttons consecutively. In jQuery, iterate over all of the options, and for each of them insert a button element into a button group. At the end, magically remove the select tag.

$("#convert").on("click", function() {
    var btnGroup = "<div class='btn-group'></div>";
    $("body").append(btnGroup);

    $("option").each(function() {
        $(".btn-group").after("<button>" + $(this).html() + "</button>");
    });

    $("#fruit-select").remove();
});
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="fruit-select">
  <option>Pineapples</option>
  <option>Mangos</option>
  <option>Watermelons</option>
</select>

<button id="convert">Convert to Button Group</button>


来源:https://stackoverflow.com/questions/37966637/how-can-i-change-select-tag-option-into-button-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!