jQuery Mobile 表单输入
系统梳理 jQuery Mobile 中文本/日期/邮箱输入框、自动扩展高度的 textarea、HTML5 搜索框、基于 fieldset[data-role='controlgroup'] 的单选与复选按钮组、水平组合、字段容器包裹、预选中与弹窗表单写法。
学完这一章,你能够
- 系统梳理 jQuery Mobile 中文本/日期/邮箱输入框、自动扩展高度的 textarea、HTML5 搜索框、基于 fieldset[data-role='controlgroup'] 的单选与复选按钮组、水平组合、字段容器包裹、预选中与弹窗表单写法。
文本输入、HTML5 类型、文本域与搜索框
jQuery Mobile 中的输入控件直接基于标准 HTML `<input>` 编写,框架会自动增强样式以优化移动端触控体验,并兼容 HTML5 新增输入类型(如 `type="text"`、`type="date"`、`type="email"`)。配合 `placeholder` 属性即可给出期望输入格式的简短占位提示。需要指出,随着 jQuery Mobile 归档停止维护,现代移动浏览器已原生提供日期选择器、邮箱软键盘与样式定制能力,无需再依赖旧版框架脚本封装。
多行文本输入使用标准 `<textarea>` 元素;在 jQuery Mobile 增强后,文本域会随用户键入内容自动扩展高度以容纳新增行(现代 CSS 对应 `field-sizing: content` 或轻量 `input` 事件高度计算)。用于搜索场景时则采用 HTML5 `<input type="search">`,框架默认附带放大镜样式与右侧清除按钮。
单选按钮与复选框控件组
当用户只能从有限选项中择一时使用单选按钮(`type="radio"`),且同一组选项必须共享相同的 `name` 属性。推荐用 `<fieldset data-role="controlgroup">` 包裹整组成对的 `<label>` 与 `<input>`,并用 `<legend>` 定义组标题,使多个选项在视觉上紧密衔接为一个圆角控件组。
当允许用户在有限选项中多选(一个或多个)时使用复选框(`type="checkbox"`),同样放入 `<fieldset data-role="controlgroup">` 与 `<legend>` 中即可获得统一的触摸按钮列表样式。
水平排列、字段容器包裹、预选中与弹窗表单
控件组支持多种常见组合方式:给 `<fieldset data-role="controlgroup">` 增加 `data-type="horizontal"` 可将单选按钮或复选框改为水平并排的分段按钮条;外层套入 `<div class="ui-field-contain">` 可让 `<legend>` 标题在宽屏下与控件组同行对齐;在 `<input type="radio">` 或 `<input type="checkbox">` 上添加标准 HTML `checked` 属性即可设置默认选中项。此外,还可以将登录表单嵌入 `<div data-role="popup" id="myPopup" class="ui-content">` 中,并通过 `<a href="#myPopup" data-rel="popup" class="ui-btn ui-btn-inline">` 触发弹出,表单内配合 `ui-hidden-accessible` 标签与 `type="password"` 密码框即可构成紧凑的弹窗登录框。
运行 jQuery Mobile 输入类型、单复选控件组与弹窗表单离线 DOM 验证
展开查看收起
"""Offline jQuery Mobile Form Inputs DOM & controlgroup verifier (Chapter 19)."""
from __future__ import annotations
import json
from lxml import html
def main() -> None:
cases = []
# Case 1: text_date_email_placeholder
markup1 = """
<form method="post" action="demo_form.php">
<div class="ui-field-contain">
<label for="fullname">全名:</label>
<input type="text" name="fullname" id="fullname">
<label for="bday">生日:</label>
<input type="date" name="bday" id="bday">
<label for="email">E-mail:</label>
<input type="email" name="email" id="email" placeholder="你的电子邮箱..">
</div>
</form>
"""
doc1 = html.fragment_fromstring(markup1, create_parent=False)
actual1 = {
"input_types": doc1.xpath(".//input/@type"),
"placeholder_email": doc1.xpath("string(.//input[@id='email']/@placeholder)"),
"labels_matched": doc1.xpath(".//label/@for") == doc1.xpath(".//input/@id"),
}
expected1 = {
"input_types": ["text", "date", "email"],
"placeholder_email": "你的电子邮箱..",
"labels_matched": True,
}
cases.append({
"name": "text_date_email_placeholder",
"summary": "验证 HTML5 text/date/email 输入类型、label 对应关系及 placeholder 属性。",
"expected": expected1,
"actual": actual1,
"passed": actual1 == expected1,
})
# Case 2: textarea_and_search_inputs
markup2 = """
<form method="post" action="demo_form.php">
<div class="ui-field-contain">
<label for="info">附加信息:</label>
<textarea name="addinfo" id="info"></textarea>
<label for="search">搜索:</label>
<input type="search" name="search" id="search">
</div>
</form>
"""
doc2 = html.fragment_fromstring(markup2, create_parent=False)
actual2 = {
"has_textarea": bool(doc2.xpath(".//textarea[@id='info'][@name='addinfo']")),
"search_type": doc2.xpath("string(.//input[@id='search']/@type)"),
"in_field_contain": len(doc2.xpath(".//div[@class='ui-field-contain']/*[self::textarea or self::input]")),
}
expected2 = {
"has_textarea": True,
"search_type": "search",
"in_field_contain": 2,
}
cases.append({
"name": "textarea_and_search_inputs",
"summary": "验证多行文本域 textarea 与 HTML5 type='search' 搜索框在字段容器内的结构。",
"expected": expected2,
"actual": actual2,
"passed": actual2 == expected2,
})
# Case 3: radio_and_checkbox_controlgroups
markup3 = """
<form method="post" action="demo_form.php">
<fieldset data-role="controlgroup" id="cg_radio">
<legend>Choose your gender:</legend>
<label for="male">Male</label>
<input type="radio" name="gender" id="male" value="male">
<label for="female">Female</label>
<input type="radio" name="gender" id="female" value="female">
</fieldset>
<fieldset data-role="controlgroup" id="cg_check">
<legend>Choose as many favorite colors as you'd like:</legend>
<label for="red">Red</label>
<input type="checkbox" name="favcolor" id="red" value="red">
<label for="green">Green</label>
<input type="checkbox" name="favcolor" id="green" value="green">
<label for="blue">Blue</label>
<input type="checkbox" name="favcolor" id="blue" value="blue">
</fieldset>
</form>
"""
doc3 = html.fragment_fromstring(markup3, create_parent=False)
radio_names = set(doc3.xpath(".//fieldset[@id='cg_radio']/input/@name"))
actual3 = {
"radio_shared_name": radio_names == {"gender"},
"radio_values": doc3.xpath(".//fieldset[@id='cg_radio']/input/@value"),
"checkbox_values": doc3.xpath(".//fieldset[@id='cg_check']/input/@value"),
"both_controlgroup": doc3.xpath(".//fieldset/@data-role") == ["controlgroup", "controlgroup"],
}
expected3 = {
"radio_shared_name": True,
"radio_values": ["male", "female"],
"checkbox_values": ["red", "green", "blue"],
"both_controlgroup": True,
}
cases.append({
"name": "radio_and_checkbox_controlgroups",
"summary": "验证 fieldset[data-role='controlgroup'] 下单选按钮同名互斥与复选框多选结构。",
"expected": expected3,
"actual": actual3,
"passed": actual3 == expected3,
})
# Case 4: horizontal_fieldcontain_checked
markup4 = """
<div class="ui-field-contain">
<fieldset data-role="controlgroup" data-type="horizontal">
<legend>请选择您的性别:</legend>
<input type="radio" name="g" id="r1" value="m" checked>
<input type="checkbox" name="c" id="c1" value="red" checked>
</fieldset>
</div>
"""
doc4 = html.fragment_fromstring(markup4, create_parent=False)
actual4 = {
"wrapped_in_field_contain": bool(doc4.xpath("self::div[@class='ui-field-contain']/fieldset[@data-role='controlgroup']")),
"orientation": doc4.xpath("string(.//fieldset/@data-type)"),
"preselected_ids": [el.get("id") for el in doc4.xpath(".//input") if "checked" in el.attrib],
}
expected4 = {
"wrapped_in_field_contain": True,
"orientation": "horizontal",
"preselected_ids": ["r1", "c1"],
}
cases.append({
"name": "horizontal_fieldcontain_checked",
"summary": "验证 data-type='horizontal' 水平组合、ui-field-contain 包裹与 checked 预选中状态。",
"expected": expected4,
"actual": actual4,
"passed": actual4 == expected4,
})
# Case 5: popup_login_form_markup
markup5 = """
<div>
<a href="#myPopup" data-rel="popup" class="ui-btn ui-btn-inline">Show Popup Form</a>
<div data-role="popup" id="myPopup" class="ui-content">
<form method="post" action="demoform.php">
<div>
<h3>登录信息</h3>
<label for="usrnm" class="ui-hidden-accessible">用户名:</label>
<input type="text" name="user" id="usrnm" placeholder="用户名">
<label for="pswd" class="ui-hidden-accessible">密码:</label>
<input type="password" name="passw" id="pswd" placeholder="密码">
</div>
</form>
</div>
</div>
"""
doc5 = html.fragment_fromstring(markup5, create_parent=False)
href = doc5.xpath("string(.//a/@href)")
popup_id = doc5.xpath("string(.//div[@data-role='popup']/@id)")
actual5 = {
"trigger_rel": doc5.xpath("string(.//a/@data-rel)"),
"target_matches_popup": href == f"#{popup_id}",
"hidden_label_count": len(doc5.xpath(".//label[@class='ui-hidden-accessible']")),
"input_types": doc5.xpath(".//input/@type"),
}
expected5 = {
"trigger_rel": "popup",
"target_matches_popup": True,
"hidden_label_count": 2,
"input_types": ["text", "password"],
}
cases.append({
"name": "popup_login_form_markup",
"summary": "验证弹窗表单触发链接 data-rel='popup' 与弹窗内无障碍隐藏标签及密码输入框结构。",
"expected": expected5,
"actual": actual5,
"passed": actual5 == expected5,
})
print(json.dumps({
"version": "CPython 3.12.14 / lxml offline jQuery Mobile DOM & reference verifier",
"cases": cases,
}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
本地运行
解压后按 README 说明在对应本地环境运行脚本。
本章附件与实例包
常见错误与边界检查
- 实际端口、文档资源与应用配置都应和测试条件一致。
技术规范与官方资料
练习与参考解答
查看参考解答
在包裹单选按钮的 <fieldset data-role="controlgroup"> 上添加 data-type="horizontal",并在第一个 <input type="radio"> 元素上添加 HTML 原生 checked 属性。