HTML 选择选项中的换行符?

发布于 2024-09-02 19:33:51 字数 33 浏览 2 评论 0原文

我可以在 html 选择选项中包含两行文本吗?如何?

Can I have a two line text in an html select option? How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

预谋 2024-09-09 19:33:51

我知道这是一篇较旧的文章,但我将其留给其他将来查看的人。

您不能将换行符格式化为选项;但是,您可以使用标题属性提供鼠标悬停工具提示以提供完整信息。在选项文本中使用简短的描述符,并在标题中给出完整的内容。

<select>
  <option
    title="This is my lengthy explanation of what this selection really means, so since you only see 1 on the drop down list you really know that you're opting to elect me as King of Willywarts!  Always be sure to read the fine print!"
    value="1">1</option>
</select>

I know this is an older post, but I'm leaving this for whomever else comes looking in the future.

You can't format line breaks into an option; however, you can use the title attibute to give a mouse-over tooltip to give the full info. Use a short descriptor in the option text and give the full skinny in the title.

<select>
  <option
    title="This is my lengthy explanation of what this selection really means, so since you only see 1 on the drop down list you really know that you're opting to elect me as King of Willywarts!  Always be sure to read the fine print!"
    value="1">1</option>
</select>

瑾兮 2024-09-09 19:33:51

将长文本放在正下方的另一个 中并禁用它怎么样?对于在这里发帖的人来说可能是一个解决方法。

        <select>
            <option>My real option text</option>
            <option disabled style="font-style:italic">   (...and my really really long option text that is basically a continuation of previous option)</option> 
            <option disabled style="font-style:italic">   (...and still continuing my previous option)</option> 
            <option>Another real option text</option>
        </select>

How about putting the long text in another <option> right below and disabling it? Might be a workaround for someone so posting here.

        <select>
            <option>My real option text</option>
            <option disabled style="font-style:italic">   (...and my really really long option text that is basically a continuation of previous option)</option> 
            <option disabled style="font-style:italic">   (...and still continuing my previous option)</option> 
            <option>Another real option text</option>
        </select>

霞映澄塘 2024-09-09 19:33:51

不,浏览器不提供此格式选项。

您可能可以使用一些带有 的复选框来伪造它,并使用 JS 将其变成飞出菜单。

No, browsers don't provide this formatting option.

You could probably fake it with some checkboxes with <label>s, and JS to turn it into a fly out menu.

穿透光 2024-09-09 19:33:51

有点黑客,但这给出了多行选择的效果,为多行添加灰色背景颜色,
如果您选择任何灰色文本,它将选择第一个分组。
我想说有点聪明:)
第一个选项还展示了如何为选项添加标题标签。

 function SelectFirst(SelVal) {
   var arrSelVal = SelVal.split(",")
   if (arrSelVal.length > 1) {
     Valuetoselect = arrSelVal[0];
     document.getElementById("select1").value = Valuetoselect;
   }
 }
<select name="select1" id="select1" onchange="SelectFirst(this.value)">
  <option value="1" title="this is my long title for the yes option">Yes</option>
  <option value="2">No</option>
  <option value="2,1" style="background:#eeeeee">   This is my description for the no option</option>
  <option value="2,2" style="background:#eeeeee">   This is line 2 for the no option</option>
  <option value="3">Maybe</option>
  <option value="3,1" style="background:#eeeeee">   This is my description for Maybe option</option>
  <option value="3,2" style="background:#eeeeee">   This is line 2 for the Maybe option</option>
  <option value="3,3" style="background:#eeeeee">   This is line 3 for the Maybe option</option>
</select>

A bit of a hack, but this gives the effect of a multi-line select, puts in a gray bgcolor for your multi line,
and if you select any of the gray text, it selects the first of the grouping.
Kinda clever I'd say :)
The first option also shows how you can put a title tag in for an option as well.

 function SelectFirst(SelVal) {
   var arrSelVal = SelVal.split(",")
   if (arrSelVal.length > 1) {
     Valuetoselect = arrSelVal[0];
     document.getElementById("select1").value = Valuetoselect;
   }
 }
<select name="select1" id="select1" onchange="SelectFirst(this.value)">
  <option value="1" title="this is my long title for the yes option">Yes</option>
  <option value="2">No</option>
  <option value="2,1" style="background:#eeeeee">   This is my description for the no option</option>
  <option value="2,2" style="background:#eeeeee">   This is line 2 for the no option</option>
  <option value="3">Maybe</option>
  <option value="3,1" style="background:#eeeeee">   This is my description for Maybe option</option>
  <option value="3,2" style="background:#eeeeee">   This is line 2 for the Maybe option</option>
  <option value="3,3" style="background:#eeeeee">   This is line 3 for the Maybe option</option>
</select>

清醇 2024-09-09 19:33:51

HTML 代码

<section style="background-color:rgb(237.247.249);">
    <h2>Test of select menu (SelectboxIt plugin)</h2>
    <select name="select_this" id="testselectset">
        <option value="01">Option 1</option>
        <option value="02">Option 2</option>
        <option value="03">Option 3</option>
        <option value="04">Option 4</option>
        <option value="05">Option 5</option>
        <option value="06">Option 6</option>
        <option value="07">Option 7 with a really, really long text line that we shall use in order to test the wrapping of text within an option or optgroup</option>
        <option value="08">Option 8</option>
        <option value="09">Option 9</option>
        <option value="10">Option 10</option>
    </select>
</section>

Javascript 代码

$(function(){
    $("#testselectset").selectBoxIt({
        theme: "default",
        defaultText: "Make a selection...",
        autoWidth: false
    });
});

CSS 代码

.selectboxit-container .selectboxit, .selectboxit-container .selectboxit-options {
    width: 400px; /* Width of the dropdown button */
    border-radius:0;
    max-height:100px;
}

.selectboxit-options .selectboxit-option .selectboxit-option-anchor {
    white-space: normal;
    min-height: 30px;
    height: auto;
}

并且您必须添加一些 Jquery 库
选择框 Jquery CSS

Jquery Ui Min JS

SelectBox Js

请检查此链接
JsFiddle 链接

HTML Code

<section style="background-color:rgb(237.247.249);">
    <h2>Test of select menu (SelectboxIt plugin)</h2>
    <select name="select_this" id="testselectset">
        <option value="01">Option 1</option>
        <option value="02">Option 2</option>
        <option value="03">Option 3</option>
        <option value="04">Option 4</option>
        <option value="05">Option 5</option>
        <option value="06">Option 6</option>
        <option value="07">Option 7 with a really, really long text line that we shall use in order to test the wrapping of text within an option or optgroup</option>
        <option value="08">Option 8</option>
        <option value="09">Option 9</option>
        <option value="10">Option 10</option>
    </select>
</section>

Javascript Code

$(function(){
    $("#testselectset").selectBoxIt({
        theme: "default",
        defaultText: "Make a selection...",
        autoWidth: false
    });
});

CSS Code

.selectboxit-container .selectboxit, .selectboxit-container .selectboxit-options {
    width: 400px; /* Width of the dropdown button */
    border-radius:0;
    max-height:100px;
}

.selectboxit-options .selectboxit-option .selectboxit-option-anchor {
    white-space: normal;
    min-height: 30px;
    height: auto;
}

and you have to add some Jquery Library
select Box Jquery CSS

Jquery Ui Min JS

SelectBox Js

Please check this link
JsFiddle Link

淡淡绿茶香 2024-09-09 19:33:51

如果您使用 select2,您可以轻松构建一些适合 下拉选项所选选项 如下所示:

当您的文本选项由 | 分割

<option value="1">Pacheco|Fmcia Pacheco|11443322551</option>

那么您的脚本可能如下所示:

<script type="text/javascript">
    function formatSearch(item) {
        var selectionText = item.text.split("|");
        var $returnString = $('<span>' + selectionText[0] + '</br><b>' + selectionText[1] + '</b></br>' + selectionText[2] +'</span>');
        return $returnString;
    };
    function formatSelected(item) {
        var selectionText = item.text.split("|");
        var $returnString = $('<span>' + selectionText[0].substring(0, 21) +'</span>');
        return $returnString;
    };
    $('.select2').select2({
        templateResult: formatSearch,
        templateSelection: formatSelected
    });
</script>

您可以在下面看到结果:

在此处输入图像描述

If you're using select2 you can easily build something to fit the dropdown options and selected option likw below:

When your text option is splitted by |

<option value="1">Pacheco|Fmcia Pacheco|11443322551</option>

Then your script could be just like this one:

<script type="text/javascript">
    function formatSearch(item) {
        var selectionText = item.text.split("|");
        var $returnString = $('<span>' + selectionText[0] + '</br><b>' + selectionText[1] + '</b></br>' + selectionText[2] +'</span>');
        return $returnString;
    };
    function formatSelected(item) {
        var selectionText = item.text.split("|");
        var $returnString = $('<span>' + selectionText[0].substring(0, 21) +'</span>');
        return $returnString;
    };
    $('.select2').select2({
        templateResult: formatSearch,
        templateSelection: formatSelected
    });
</script>

The result you can see below:

enter image description here

百善笑为先 2024-09-09 19:33:51

只需像这样样式选择控件即可:

<select style="height: 50px; white-space: pre-wrap;">

结果:

在此处输入图像描述

Just style the select control like this:

<select style="height: 50px; white-space: pre-wrap;">

Result:

enter image description here

凹づ凸ル 2024-09-09 19:33:51

您可以使用带有列表属性的普通输入元素,然后添加带有值的选项,
该值将显示为第一行,选项文本将自动显示为第二行。我找到了这个解决方案,看看这个方法也可能有帮助

<label class="m-t-20">Customer List</label>
   <input name="customerlist" class="customerlist form-control" list="customerlists" >
   <datalist id="customerlists">
     <option value="Gerda Weijers">Aw Trucks Limited </option>
     <option value="Tracy Lolesi">Acorn Stoneworx Limited </option>
     <option value="Pacheco">Pacheco Fmcia Pacheco</option>
   </datalist>

但这仅适用于 Chrome,其他浏览器不显示值文本。

you can use normal input element with list attribute, and then add options with value,
the value will be displayed to as a first line and the option text will be displayed as second line automatically. I found this solution, have a look, this way may also help

<label class="m-t-20">Customer List</label>
   <input name="customerlist" class="customerlist form-control" list="customerlists" >
   <datalist id="customerlists">
     <option value="Gerda Weijers">Aw Trucks Limited </option>
     <option value="Tracy Lolesi">Acorn Stoneworx Limited </option>
     <option value="Pacheco">Pacheco Fmcia Pacheco</option>
   </datalist>

but this only works in Chrome, other browsers don't show value text.

花辞树 2024-09-09 19:33:51

无法在所有浏览器上完全正常工作(hr行部分),但解决方案如下:

<select name="selector">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
  <option value="3">Option 3</option>
  <option disabled><hr></option>
  <option value="4">Option 4</option>
  <option value="5">Option 5</option>
  <option value="6">Option 6</option>
</select>

Does not work fully (the hr line part) on all browsers, but here is the solution:

<select name="selector">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
  <option value="3">Option 3</option>
  <option disabled><hr></option>
  <option value="4">Option 4</option>
  <option value="5">Option 5</option>
  <option value="6">Option 6</option>
</select>

[浮城] 2024-09-09 19:33:51

相反,也许尝试用标记替换选择,例如

// Wrap any selects that should be replaced
$('select.replace').wrap('<div class="select-replacement-wrapper"></div>');

// Replace placeholder text with select's initial value
$('.select-replacement-wrapper').each(function() {
	$(this).prepend('<a>' + $('select option:selected', this).text() + '</a>');
});

// Update placeholder text with updated select value
$('select.replace').change(function(event) {
  $(this).siblings('a').text( $('option:selected', this).text() );
});
/* Position relative, so that we can overlay the hidden select */
.select-replacement-wrapper {
  position: relative;
  border: 3px solid red; /* to show area */
  width: 33%;
  margin: 0 auto;
}

/* Only shows if JS is enabled */
.select-replacement-wrapper a {
  /* display: none; */
  /* Notice that we've centered this text - 
   you can do whatever you want, mulitple lines wrap, etc, 
   since this is not a select element' */
  text-align: center;
  display: block;
  border: 1px solid blue;
}

select.replace {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  border: 1px solid green;
  opacity: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>


<select id="dropdown" class="replace">
  <option value="First">First</option>
  <option value="Second" selected>Second, and this is a long line, just to show wrapping</option>
  <option value="Third">Third</option>
</select>

Instead, maybe try replacing the select with markup, e.g.

// Wrap any selects that should be replaced
$('select.replace').wrap('<div class="select-replacement-wrapper"></div>');

// Replace placeholder text with select's initial value
$('.select-replacement-wrapper').each(function() {
	$(this).prepend('<a>' + $('select option:selected', this).text() + '</a>');
});

// Update placeholder text with updated select value
$('select.replace').change(function(event) {
  $(this).siblings('a').text( $('option:selected', this).text() );
});
/* Position relative, so that we can overlay the hidden select */
.select-replacement-wrapper {
  position: relative;
  border: 3px solid red; /* to show area */
  width: 33%;
  margin: 0 auto;
}

/* Only shows if JS is enabled */
.select-replacement-wrapper a {
  /* display: none; */
  /* Notice that we've centered this text - 
   you can do whatever you want, mulitple lines wrap, etc, 
   since this is not a select element' */
  text-align: center;
  display: block;
  border: 1px solid blue;
}

select.replace {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  border: 1px solid green;
  opacity: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>


<select id="dropdown" class="replace">
  <option value="First">First</option>
  <option value="Second" selected>Second, and this is a long line, just to show wrapping</option>
  <option value="Third">Third</option>
</select>

冬天的雪花 2024-09-09 19:33:51

好的,我找到了一个解决方案:

HTML:

<div class="styled-select">
    <select class="select-css">
        <option disabled selected></option>
        <option>Apples</option>
        <option>Bananas</option>
        <option>Grapes</option>
        <option>Oranges</option>
    </select>
    <span>How many kg's per week do you expect to be ordering</span>
</div>

CSS:

.styled-select select.select-css {
appearance: none;
   height: 80px;
   pointer-events:all;
   position:absolute;
   top:0;
   left:0;
}
.styled-select {
    position:relative;
    appearance: none;
    overflow: hidden;
    pointer-events:none;
   }

jQuery:

$(".select-css").on("change", function(){
    $(this).next('span').css('display', 'none');
});

Ok i found a solution:

HTML:

<div class="styled-select">
    <select class="select-css">
        <option disabled selected></option>
        <option>Apples</option>
        <option>Bananas</option>
        <option>Grapes</option>
        <option>Oranges</option>
    </select>
    <span>How many kg's per week do you expect to be ordering</span>
</div>

CSS:

.styled-select select.select-css {
appearance: none;
   height: 80px;
   pointer-events:all;
   position:absolute;
   top:0;
   left:0;
}
.styled-select {
    position:relative;
    appearance: none;
    overflow: hidden;
    pointer-events:none;
   }

jQuery:

$(".select-css").on("change", function(){
    $(this).next('span').css('display', 'none');
});
浮云落日 2024-09-09 19:33:51

您可以使用名为 select2 的库

您也可以查看此 Stackoverflow 问题 &答案

<select id="selectBox" style="width: 500px">
  <option value="1" data-desc="this is my <br> multiple line 1">option 1</option>
  <option value="2" data-desc="this is my <br> multiple line 2">option 2</option>
</select>

在 JavaScript 中

$(function(){
  $("#selectBox").select2({
    templateResult: formatDesc
  });

  function formatDesc (opt) {
   var optdesc = $(opt.element).attr('data-desc'); 
    var $opt = $(
      '<div><strong>' + opt.text + '</strong></div><div>' + optdesc + '</div>'
    );
    return $opt;
  };
});

You can use a library called select2

You also can look at this Stackoverflow Question & Answer

<select id="selectBox" style="width: 500px">
  <option value="1" data-desc="this is my <br> multiple line 1">option 1</option>
  <option value="2" data-desc="this is my <br> multiple line 2">option 2</option>
</select>

In javascript

$(function(){
  $("#selectBox").select2({
    templateResult: formatDesc
  });

  function formatDesc (opt) {
   var optdesc = $(opt.element).attr('data-desc'); 
    var $opt = $(
      '<div><strong>' + opt.text + '</strong></div><div>' + optdesc + '</div>'
    );
    return $opt;
  };
});
如梦亦如幻 2024-09-09 19:33:51

一个想法可能是使用 optgroup。就我而言,发现它比禁用方法更好。我认为这比看到禁用选项更容易让用户感到困惑。

<select id="q1" v-model="selected" v-on:change="setCPost1(selected)">
  <option value="0"></option>
  <template
    v-for="(child, idx) in getLevel1"
    v-bind:value="child.id"      
  >

  <optgroup v-bind:value="child.id" :key="idx"
  :label="child.label"
  v-if="child.label_line_two"
  >
  </optgroup>

  <option v-bind:value="child.id" :key="idx"  v-if="!child.label_line_two"
  >
  {{ child.label }}  
  </option>

  <option v-bind:value="child.id" :key="idx" v-if="child.label_line_two"
  style="font-style:italic">
  {{ child.label_line_two }}
  </option>


  </template>
</select>

外部组件听起来很酷,比如 Vue Select,但我现在想坚持使用原生 html select。

An idea could be to use the optgroup. In my case found it better than the disabled approach. It's less confusing for the user than seeing the disabled option I think.

<select id="q1" v-model="selected" v-on:change="setCPost1(selected)">
  <option value="0"></option>
  <template
    v-for="(child, idx) in getLevel1"
    v-bind:value="child.id"      
  >

  <optgroup v-bind:value="child.id" :key="idx"
  :label="child.label"
  v-if="child.label_line_two"
  >
  </optgroup>

  <option v-bind:value="child.id" :key="idx"  v-if="!child.label_line_two"
  >
  {{ child.label }}  
  </option>

  <option v-bind:value="child.id" :key="idx" v-if="child.label_line_two"
  style="font-style:italic">
  {{ child.label_line_two }}
  </option>


  </template>
</select>

An external component sounds cool like Vue Select, but I wanted to stick with the native html select at the moment.

猫性小仙女 2024-09-09 19:33:51

是的,通过使用 css 样式
空白:预换行;
在引导程序的 .dropdown 类中覆盖它。
早些时候它是空白:nowrap;所以它使下拉菜单包装成一行。
预包裹使其根据宽度。

yes, by using css styles
white-space: pre-wrap;
in the .dropdown class of the bootstrap by overriding it.
Earlier it is white-space: nowrap; so it makes the dropdown wrapped into one line.
pre-wrap makes it as according to the width.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文