fix(form-select): 修复 option 两端的 Unicode 空格(U+00A0)被去除的问题 (#2676)

* fix(form-select): 修复 option 两端的 Unicode 空格(U+00A0)被去除的问题

* update

* update

* fix
2.9.x-stable
morning-star 2025-05-15 10:32:04 +08:00 committed by 贤心
parent 0a80e99024
commit 32916cc7da
1 changed files with 18 additions and 14 deletions

View File

@ -500,7 +500,7 @@ layui.define(['lay', 'layer', 'util'], function(exports){
// 未查询到相关值 // 未查询到相关值
if(none){ if(none){
initValue = $(select[0].options[selectedIndex]).text(); // 重新获得初始选中值 initValue = $(select[0].options[selectedIndex]).prop('text'); // 重新获得初始选中值
// 如果是第一项,且文本值等于 placeholder则清空初始值 // 如果是第一项,且文本值等于 placeholder则清空初始值
if(selectedIndex === 0 && initValue === input.attr('placeholder')){ if(selectedIndex === 0 && initValue === input.attr('placeholder')){
@ -706,7 +706,7 @@ layui.define(['lay', 'layer', 'util'], function(exports){
input.on('input propertychange', layui.debounce(search, 50)).on('blur', function(e){ input.on('input propertychange', layui.debounce(search, 50)).on('blur', function(e){
var selectedIndex = select[0].selectedIndex; var selectedIndex = select[0].selectedIndex;
initValue = $(select[0].options[selectedIndex]).text(); // 重新获得初始选中值 initValue = $(select[0].options[selectedIndex]).prop('text'); // 重新获得初始选中值
// 如果是第一项,且文本值等于 placeholder则清空初始值 // 如果是第一项,且文本值等于 placeholder则清空初始值
if(selectedIndex === 0 && initValue === input.attr('placeholder')){ if(selectedIndex === 0 && initValue === input.attr('placeholder')){
@ -728,6 +728,17 @@ layui.define(['lay', 'layer', 'util'], function(exports){
if(othis.hasClass(DISABLED)) return false; if(othis.hasClass(DISABLED)) return false;
// 将新增的 option 元素添加到末尾
if(isCreatable && othis.hasClass(CREATE_OPTION)){
var optionElem = $('<option>').text(othis.text());
var displayValue = optionElem.prop('text');
value = displayValue;
optionElem.attr('value', displayValue);
select.append(optionElem);
othis.removeClass(CREATE_OPTION).attr('lay-value', displayValue).text(displayValue);
dl.append(othis);
}
if(othis.hasClass('layui-select-tips')){ if(othis.hasClass('layui-select-tips')){
input.val(''); input.val('');
} else { } else {
@ -735,13 +746,6 @@ layui.define(['lay', 'layer', 'util'], function(exports){
othis.addClass(THIS); othis.addClass(THIS);
} }
// 将新增的 option 元素添加到末尾
if(isCreatable && othis.hasClass(CREATE_OPTION)){
dl.append(othis.removeClass(CREATE_OPTION));
var optionElem = $('<option>').attr('value', value).text(othis.text());
select.append(optionElem);
}
othis.siblings().removeClass(THIS); othis.siblings().removeClass(THIS);
select.val(value).removeClass('layui-form-danger'); select.val(value).removeClass('layui-form-danger');
@ -796,7 +800,7 @@ layui.define(['lay', 'layer', 'util'], function(exports){
var isCreatable = typeof othis.attr('lay-creatable') === 'string' && isSearch; var isCreatable = typeof othis.attr('lay-creatable') === 'string' && isSearch;
var isAppendTo = typeof othis.attr('lay-append-to') === 'string'; var isAppendTo = typeof othis.attr('lay-append-to') === 'string';
var placeholder = optionsFirst var placeholder = optionsFirst
? (optionsFirst.value ? TIPS : (optionsFirst.innerText || TIPS)) ? (optionsFirst.value ? TIPS : (optionsFirst.text || TIPS))
: TIPS; : TIPS;
// 用于替代 select 的外层容器 // 用于替代 select 的外层容器
@ -815,8 +819,8 @@ layui.define(['lay', 'layer', 'util'], function(exports){
var elem = $('<input type="text" class="layui-input">'); var elem = $('<input type="text" class="layui-input">');
// 设置占位符和默认值 // 设置占位符和默认值
elem.prop('placeholder', $.trim(placeholder)); elem.prop('placeholder', placeholder);
elem.val($.trim(value ? selected.text() : '')); elem.val(value ? selected.prop('text') : '');
// 设置未开启搜索或禁用时的输入框只读状态 // 设置未开启搜索或禁用时的输入框只读状态
if (!isSearch || disabled) { if (!isSearch || disabled) {
@ -850,7 +854,7 @@ layui.define(['lay', 'layer', 'util'], function(exports){
var dd = $('<dd lay-value=""></dd>'); var dd = $('<dd lay-value=""></dd>');
if (index === 0 && !item.value && tagName !== 'optgroup') { if (index === 0 && !item.value && tagName !== 'optgroup') {
dd.addClass('layui-select-tips'); dd.addClass('layui-select-tips');
dd.text($.trim(item.innerText || TIPS)); dd.text(item.text || TIPS);
arr.push(dd.prop('outerHTML')); arr.push(dd.prop('outerHTML'));
} else if(tagName === 'optgroup') { } else if(tagName === 'optgroup') {
var dt = $('<dt></dt>'); var dt = $('<dt></dt>');
@ -864,7 +868,7 @@ layui.define(['lay', 'layer', 'util'], function(exports){
if (item.disabled) { if (item.disabled) {
dd.addClass(DISABLED); dd.addClass(DISABLED);
} }
dd.text($.trim(item.innerText)); dd.text(item.text);
arr.push(dd.prop('outerHTML')); arr.push(dd.prop('outerHTML'));
} }
}); });