蓝色天空.知识 · 实践 · 创造
我的收藏
Ionic 教程目录 37章
Ionic / LEARN BY DOING

Ionic 单选框

解析 Ionic 1 单选框(item-radio)的标准结构、相同 name 属性的互斥分组机制、radio-icon 选中勾选图标显示原理、label 整行触控体验以及现代 ion-radio-group 对照。

学完这一章,你能够

  • 解析 Ionic 1 单选框(item-radio)的标准结构、相同 name 属性的互斥分组机制、radio-icon 选中勾选图标显示原理、label 整行触控体验以及现代 ion-radio-group 对照。

Ionic 单选框结构、name 互斥组与触控设计

Ionic 1 单选框(Radio)基于标准 `<input type="radio">` 打造出贴合移动应用习惯的勾选列表样式。其核心结构为:`<div class="list">` 内每个候选项均使用 `<label class="item item-radio">` 包裹(利用 `<label>` 原生关联机制让整行任意位置都可轻松点击),内部放置共享同一 `name` 属性(如 `name="group"`)的单选框以保证组内互斥,并通过 `checked="checked"` 设定默认选中项(示例涵盖 Go、Python、Ruby、.Net、Java、PHP 六个选项);紧随其后的 `<div class="radio-content">` 负责呈现 `<div class="item-content">` 文本与右侧 `<i class="radio-icon ion-checkmark"></i>` 选中标记(仅当对应 radio 处于选中态时由 CSS 显示该对勾图标)。源页面的内嵌演示 `iframe` 仅做静态登记;现代 Ionic 7/8 则通过 `<ion-radio-group value="go">` 与 `<ion-radio value="...">` 声明式管理单选状态。

运行 Ionic 1 单选框分组 DOM、互斥选中状态机与 label 热区离线核验

含完整源码 · 162 行
展开查看收起
python
"""Offline Ionic 1 Radio group DOM, shared name grouping & exclusive selection verifier for Ch11 (radio)."""
from __future__ import annotations

import json
from lxml import html

VERSION = "CPython 3.12.14 / lxml offline Ionic UI & component verifier"

RADIO_HTML = """
<div class="list">
  <label class="item item-radio">
    <input type="radio" name="group" value="go" checked="checked">
    <div class="radio-content">
      <div class="item-content">Go</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
  <label class="item item-radio">
    <input type="radio" name="group" value="python">
    <div class="radio-content">
      <div class="item-content">Python</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
  <label class="item item-radio">
    <input type="radio" name="group" value="ruby">
    <div class="radio-content">
      <div class="item-content">Ruby</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
  <label class="item item-radio">
    <input type="radio" name="group" value=".net">
    <div class="radio-content">
      <div class="item-content">.Net</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
  <label class="item item-radio">
    <input type="radio" name="group" value="java">
    <div class="radio-content">
      <div class="item-content">Java</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
  <label class="item item-radio">
    <input type="radio" name="group" value="php">
    <div class="radio-content">
      <div class="item-content">PHP</div>
      <i class="radio-icon ion-checkmark"></i>
    </div>
  </label>
</div>
"""


def make_case(name: str, summary: str, expected, actual) -> dict:
    return {
        "name": name,
        "summary": summary,
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_radio_group_dom() -> dict:
    root = html.fragment_fromstring(RADIO_HTML)
    labels = root.xpath("./label[contains(@class,'item-radio')]")
    names = {lbl.xpath("./input/@name")[0] for lbl in labels}
    values = [lbl.xpath("./input/@value")[0] for lbl in labels]
    texts = [lbl.xpath("./div[@class='radio-content']/div[@class='item-content']/text()")[0].strip() for lbl in labels]
    icons = {lbl.xpath(".//i/@class")[0] for lbl in labels}
    initial_checked = [lbl.xpath("./input/@value")[0] for lbl in labels if lbl.xpath("./input/@checked")]
    actual = {
        "option_count": len(labels),
        "shared_name_set": sorted(names),
        "values": values,
        "labels": texts,
        "icon_classes": sorted(icons),
        "initial_checked": initial_checked,
    }
    expected = {
        "option_count": 6,
        "shared_name_set": ["group"],
        "values": ["go", "python", "ruby", ".net", "java", "php"],
        "labels": ["Go", "Python", "Ruby", ".Net", "Java", "PHP"],
        "icon_classes": ["radio-icon ion-checkmark"],
        "initial_checked": ["go"],
    }
    return make_case(
        "radio_group_markup_and_shared_name",
        "Verify 6 label.item.item-radio rows share name='group', .radio-content/.item-content, and .radio-icon",
        expected,
        actual,
    )


def case_radio_mutual_exclusion_machine() -> dict:
    root = html.fragment_fromstring(RADIO_HTML)
    values = [lbl.xpath("./input/@value")[0] for lbl in root.xpath("./label")]
    selected = "go"

    def select(val: str) -> dict:
        nonlocal selected
        assert val in values
        selected = val
        return {v: (v == selected) for v in values}

    state_after_python = select("python")
    state_after_java = select("java")
    actual = {
        "checked_count_after_python": sum(state_after_python.values()),
        "python_visible_checkmark": state_after_python["python"] and not state_after_python["go"],
        "final_selected": selected,
        "checked_count_after_java": sum(state_after_java.values()),
    }
    expected = {
        "checked_count_after_python": 1,
        "python_visible_checkmark": True,
        "final_selected": "java",
        "checked_count_after_java": 1,
    }
    return make_case(
        "radio_mutual_exclusion_and_icon_visibility",
        "Verify mutual exclusion invariant (exactly one checked option and visible checkmark) on selection changes",
        expected,
        actual,
    )


def case_radio_label_ergonomics_and_modern_mapping() -> dict:
    root = html.fragment_fromstring(RADIO_HTML)
    all_wrapped_in_label = all(el.tag == "label" for el in root.xpath("./*"))
    actual = {
        "all_options_use_label_wrapper": all_wrapped_in_label,
        "modern_component": "<ion-radio-group value=\"go\"><ion-radio value=\"go\">Go</ion-radio></ion-radio-group>",
    }
    expected = {
        "all_options_use_label_wrapper": True,
        "modern_component": "<ion-radio-group value=\"go\"><ion-radio value=\"go\">Go</ion-radio></ion-radio-group>",
    }
    return make_case(
        "radio_label_touch_target_and_modern_mapping",
        "Verify <label> full-row touch target wrapper and Ionic 7/8 <ion-radio-group> equivalence",
        expected,
        actual,
    )


def main() -> None:
    cases = [
        case_radio_group_dom(),
        case_radio_mutual_exclusion_machine(),
        case_radio_label_ergonomics_and_modern_mapping(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
验证 6 个选项共享 `name="group"`、默认选中 `go`、切换选项时严格满足唯一选中与 `.radio-icon` 联动,以及 `<label>` 整行包裹与现代 `<ion-radio-group>` 映射。

查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载本章实验包 (mbskycom202612a2013.zip) ↓ 查看运行说明 ↗

本章附件与实例包

下载课程关联工程包:Ionic

课程关联工程包(登记关联37章) · 含当前章节或实例 · mbskycom202612a2013.zip

下载课程关联工程包:Ionic

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2043.zip

常见错误与边界检查

  • 实际端口、文档资源与应用配置都应和测试条件一致。

技术规范与官方资料

练习与参考解答

查看参考解答

浏览器仅对相同 `name` 的单选框执行互斥约束;若 `name` 不同,用户将可以同时勾选多个选项,破坏单选语义。

SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习