我正在使用选择2下拉菜单,然后在其内容中显示一些长句子。我想在句子的正确位置添加换行符,但是下拉列表是自动调整的。
该行现在看起来像这样
选择2期$ 100。(特别
提供。)
我需要添加受控的换行符,使其看起来像这样:
选择2期$ 100。
(特价。)
我不要增加下拉菜单的宽度或更改字体大小。
我的代码在jsfiddle上:
<select multiple id="e1" style="width:300px">
<option value="1">select 1 installment of $200</option>
<option value="2">select 2 installments of $100. (special offer.)</option>
<option value="3">select 3 installments of $89</option>
<option value="4">select 4 installments of $50. (no interest in this option)</option>
<option value="5">select 5 installments of $45</option>
</select>
对于select2版本3.5或更低版本,我使用属性“ formatResult”和“ formatSelection”解决了该问题。
如果您使用的是v4.0或更高版本,请改用“ templateResult”和“ templateSelection”。并在回调函数中使用jquery标记,以便不会对换行符的html标记进行过滤。
此处解决的jsfiddle在select2 v3.5及以下版本中显示了它。
我在javascript中这样声明了select2下拉列表:
$('#color').select2({
placeholder: "Select something",
minimumResultsForSearch: Infinity, //removes the search box
formatResult: formatDesign,
formatSelection: formatDesign
});
然后在回调函数-formatDesign()中,我拆分所有字符串并在其中添加br标签,如下所示
function formatDesign(item) {
var selectionText = item.text.split(".");
var $returnString = selectionText[0] + "</br>" + selectionText[1];
return $returnString;
};
(注意:对于v4.0及更高版本,请使用jquery字符串,将br添加到字符串中。它看起来像这样:)
var $returnString = $('<span>'+selectionText[0] + '</br>' + selectionText[1] + '</span>');
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句