在不同行显示多个选择选项

时间:2015-04-02 12:37:14

标签: javascript jquery html css

我有一个多选项,在div容器中显示结果,当点击ctrl +“orange”“apple”“banana”结果显示:“orange,apple,banana”在一行,但我想要将每个结果显示在一个新的单行中,其链接将转到不同的页面,如下所示:

橙色 - “进入链接”
Apple - “转到链接”
香蕉 - “去链接”

以下是我的代码:

<script src="jquery-mobile/jquery-1.8.3.min.js" type="text/javascript">

</script>
<select name="" multiple="multiple" id="select-custom-19">    
    <option>Add Fruits</option>        
    <option value="orange" selected>orange</option> 
    <option value="apple">apple</option>
    <option value="banana">banana</option>             </select>
<div style="width:300px; height:70px; background-color:#E1D903; 
        color:#000000; margin: 10px; "> <span id="yourfruit"> </span>

</div>
<script type="text/javascript">
    $(document).ready(function() {

        $('#select-custom-19').change(function() {
            /* setting currently changed option value to option variable */
            var option = $(this).find('option:selected').val();
            /* setting input box value to selected option value */
            $('#yourfruit').text($(this).val());
        });
    });
</script>

您的帮助将受到高度赞赏。

由于

4 个答案:

答案 0 :(得分:1)

您可以尝试在每个选定的选项后添加<br/>元素。我使用了<label>元素,但您可以添加链接或任何其他您想要的元素

$(document).ready( function ()
     {
     $('#select-custom-19').change(function(){

        $('#yourfruit').empty();

        var values = $(this).val();

        for(var i=0; i < values.length ; i++)
        {
          $('#yourfruit').append('<lable>'+values[i]+'</label><br/>');
        }   
      });
 });

<强> JSFiddle Demo

答案 1 :(得分:0)

尝试以下

<script src="jquery-mobile/jquery-1.8.3.min.js" type="text/javascript"></script>
<select name="" multiple="multiple" id="select-custom-19">
  <option>Add Fruits</option>
  <option value="orange" selected>orange</option>
  <option value="apple">apple</option>
  <option value="banana">banana</option>
</select>
<div style="width:300px; height:70px; background-color:#E1D903;  color:#000000; margin: 10px; ">
  <ul id="yourfruit">
  </ul>
</div>
<script type="text/javascript">
    $(document).ready( function ()
     {

           $('#select-custom-19').change(function()
           {
        /* setting currently changed option value to option variable */
        var option = $(this).find('option:selected').val();
        /* setting input box value to selected option value */
        $('#yourfruit').append('<li>'+$(this).val()+'</li>');
            }
              );
                 });
    </script>

答案 2 :(得分:0)

您可以根据自己的要求添加HTML代码,例如<a></br>

&#13;
&#13;
$(document).ready(function () {
    $('#select-custom-19').change(function () {
        $('#yourfruit').text("");
        if($(this).val() != null)
        {
           $(this).val().forEach(function (value, index) {
               $('#yourfruit').append("<a href='#'>" + value + "</a></br>");
           });
        }
    });
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<select name="" multiple="multiple" id="select-custom-19">
    <option>Add Fruits</option>
    <option value="orange" selected>orange</option>
    <option value="apple">apple</option>
    <option value="banana">banana</option>
</select>
<div style="width:300px; height:70px; background-color:#E1D903; 
    color:#000000; margin: 10px; "> <span id="yourfruit"> </span>

</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您可以迭代throug项目并显示它们,附加一个锚点(<a />)并使用<br />换行。

请务必添加.empty()以清除.append()其他项目之前的列表中的水果到$('#yourfruit'),如下例所示。

var fruits = $(this).val();
$('#yourfruit').empty();
for (var fruit in fruits) {
   var label = $('<label/> ').text(fruits[fruit]+" - ");
   $('#yourfruit').append(label)
                   .append($('<a  class="tab-btn" />').text(fruits[fruit]).attr('href', fruits[fruit] + '.html'))
                  .append('<br />');
}

&#13;
&#13;
$(document).ready(function() {
  $('#select-custom-19').change(function() {
    /* setting currently changed option value to option variable */
    var option = $(this).find('option:selected').val();
    /* setting input box value to selected option value */
    var fruits = $(this).val();
    $('#yourfruit').empty();
    for (var fruit in fruits) {
        var label = $('<label/> ').text(fruits[fruit] + " - ");
        $('#yourfruit').append(label)
                       .append($('<a  class="tab-btn" />').text(fruits[fruit]).attr('href', fruits[fruit] + '.html'))
                       .append('<br />');
    }

  });
});
&#13;
.tab-btn {
  color: red;
}
.tab-btn:hover {
  color: blue;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" multiple="multiple" id="select-custom-19">
  <option>Add Fruits</option>

  <option value="orange" selected>orange</option>
  <option value="apple">apple</option>
  <option value="banana">banana</option>

</select>

<div style="width:300px; height:70px; background-color:#E1D903; 
    color:#000000; margin: 10px; ">
  <span id="yourfruit"> </span>
</div>
&#13;
&#13;
&#13;