Skip to content

复选框

复选框控件用于在一组选项中选择多个值。

属性

sg-caption

  • 说明:复选框组的标签文字
  • 类型string

示例

json
"sg-caption":"请选择:"

sg-captionclass

  • 说明:标签文字宽度样式,w1-w7 对应 1-7 个字符宽度,more 表示超出部分省略显示(如 w5 more
  • 类型string

示例

json
"sg-captionclass":"w5"

sg-label

  • 说明:单个复选框的显示文字
  • 类型string

示例

json
"sg-label":"复选"

value

  • 说明:单个复选框的值
  • 类型string

示例

json
"value":"1"

方法

getCheckBoxValList

获取 checkbox 选中值,返回数组格式。

语法

js
getCheckBoxValList(name)

参数

参数名类型必填说明
namestringcheckbox 控件的 name

返回值

Array - 选中的 checkbox 值列表。

示例

js
// 获取 name 为 hobbies 的复选框选中值列表
var hobbies = getCheckBoxValList("hobbies");
console.log("选中的爱好: " + hobbies.join(", "));

getCheckBoxValStr

获取 checkbox 选中值,返回字符串格式。

语法

js
getCheckBoxValStr(name, separator)

参数

参数名类型必填说明
namestringcheckbox 控件的 name
separatorstring分隔符

返回值

string - 使用指定分隔符连接的选中值字符串。

示例

js
// 获取 name 为 hobbies 的复选框选中值,用逗号分隔
var hobbies = getCheckBoxValStr("hobbies", ",");
console.log("选中的爱好: " + hobbies);

setCheckBoxVal

以数组形式为 checkbox 赋值。

语法

js
setCheckBoxVal(name, list)

参数

参数名类型必填说明
namestringcheckbox 控件的 name
listArray值列表

示例

js
// 设置 name 为 hobbies 的复选框选中值
setCheckBoxVal("hobbies", ["reading", "music", "sports"]);

setCheckBoxValStr

以字符串形式为 checkbox 赋值。

语法

js
setCheckBoxValStr(name, str, separator)

参数

参数名类型必填说明
namestringcheckbox 控件的 name
strstring值字符串
separatorstring分隔符

示例

js
// 设置 name 为 hobbies 的复选框选中值,值为逗号分隔的字符串
setCheckBoxValStr("hobbies", "reading,music,sports", ",");

setCheckChecked

设置单个 checkbox 的勾选状态。

语法

js
setCheckChecked(selector, status)

参数

参数名类型必填说明
selectorstring选择器(如 "#id" 或 ".class")
statusboolean状态(true 勾选 / false 取消勾选)

示例

js
// 勾选 id 为 agree 的复选框
setCheckChecked("#agree", true);

// 取消勾选 class 为 terms 的复选框
setCheckChecked(".terms", false);

getCheckChecked

判断单个 checkbox 是否勾选。

语法

js
getCheckChecked(selector, mode)

参数

参数名类型必填说明
selectorstring选择器(如 "#id" 或 ".class")
modestring模式

返回值

boolean - true 表示勾选,false 表示未勾选。

示例

js
// 检查 id 为 agree 的复选框是否勾选
var isChecked = getCheckChecked("#agree", "normal");
if (isChecked) {
    console.log("用户已同意条款");
} else {
    console.log("用户未同意条款");
}

基于 MIT 许可发布