feat(form): 相同 name 的 checkbox,将值序列化为数组 (#2428)

* fix(form): form.val 获取 multiple select 值错误

* fix(form): 表单验证时非字符串值不应规范化

* feat(form): 相同 name 属性的多选框,支持将值序列化为数组

* update

---------

Co-authored-by: 贤心 <3277200+sentsim@users.noreply.github.com>
pull/2443/head
morning-star 2024-12-25 19:07:58 +08:00 committed by GitHub
parent d833771d77
commit c7452c788a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 2 deletions

View File

@ -113,7 +113,7 @@ layui.define(['lay', 'layer', 'util'], function(exports){
type = itemElem[0].type;
// 如果为复选框
if(type === 'checkbox'){
if(itemElem.length === 1 && type === 'checkbox'){
itemElem[0].checked = value;
} else if(type === 'radio') { // 如果为单选框
itemElem.each(function(){
@ -154,7 +154,12 @@ layui.define(['lay', 'layer', 'util'], function(exports){
}
if(/^(checkbox|radio)$/.test(item.type) && !item.checked) return; // 复选框和单选框未选中,不记录字段
field[init_name || item.name] = othis.val();
var n = init_name || item.name;
var v = othis.val();
// 相同 name 的字段,将值合并到数组
field[n] = field[n] === undefined ? v
: $.isArray(field[n]) ? field[n].concat(v)
: [field[n], v];
});
return field;