getChecked(id) 获取选中状态

功能分类: UI Controls Applab JavaScript 少儿编程

获取 checkbox 多选框 或 radioButton 单选按钮 选中状态。

getChecked() 在 APP 应用里被用来获取指定 id 唯一标识的 checkbox 多选框 或 radioButton 单选按钮 选中状态。多选框通常用来让用户选择或拒绝某个选项,如接受或拒绝用户协议。而单选框则被用来让用户从几个选项中选择一个。

示例代码



// 获取并显示 checkbox 选中状态
checkbox("agreeBox", false);
textLabel("agreeLabel","I agree to the above privacy statement.","agreeBox");

textLabel("response1","Response: ");
textLabel("response2","");
onEvent("agreeBox", "click", function() {
  setText("response2",getChecked("agreeBox"));
});

示例代码:单选按钮遍历 点击按钮时,遍历并找出选中状态的单选按钮。


// 点击按钮时,遍历并找出选中状态的单选按钮。
radioButton("Red", false,"ColorGroup");
textLabel("RedLabel","Red","Red");
radioButton("Blue", false,"ColorGroup");
textLabel("BlueLabel","Blue","Blue");
radioButton("Green", false,"ColorGroup");
textLabel("GreenLabel","Green","Green");
radioButton("Orange", false,"ColorGroup");
textLabel("OrangeLabel","Orange","Orange");

button("favorite","What's my favorite color?");
onEvent("favorite","click", function() {
    var radioIDs = ["Red","Blue","Green","Orange"];
    var index = 0;
    while (index < radioIDs.length && !getChecked(radioIDs[index])) {
      index++;
    }
    console.log("Your favorite color is: " + radioIDs[index]);
});

语法规则


getChecked(id)

参数说明

名称 类型 必需 参数描述
id string Yes screen 屏幕控件 的 id 唯一标识。必须以字母开头,不能包含空格,可以包含字母、数字、减号 - 以及下划线 _ 。

返回值

布尔值,true 或 false。

提示说明

  • CheckBox 复选框 或 radioButton 单选按钮 也可以在 设计 模式下通过拖拽的方式添加到屏幕页面,并设置其属性。

查看更多少儿编程教程、JavaScript 介绍

返回文档首页