This time I will show you how to use form components in the Mobile framework. What are the precautions for using form components in the Mobile framework? Here are practical cases. Let’s take a look. .
1. Form component basics
1. Component introductionThe form component in jQuery Mobile is based on standard HTML, and then enhances the style on this basis, so even if the browser does not support jQuery Mobile forms still work fine. It should be noted that jQuery Mobile will enhance form elements into a form that is easy to use on touch devices, so using web forms for iPhone/iPad and Android will become very convenient.
The form components of jQuery Mobile include the following:
(1) Text input box, the input element marked with type="text" will be automatically enhanced to jQuery Mobile style, without adding additional data-role attributes .
(2) Text input field, the textarea element will be automatically enhanced without adding additional data-role attributes. For multi-line input text, jQuery Mobile will automatically increase the height of the text field to avoid difficulty in mobile devices. Find the scrollbar situation.
(3) Search input box, the input element marked with type="search" will be automatically enhanced without adding additional data-role attributes. This is a new HTML element. There is a magnifying glass icon on the left side of the enhanced input box. Click to trigger the search. After entering the content, a cross icon will appear on the right side of the input box. Click to clear the entered content, which is very convenient.
(4)Radio button, the input element marked with type="radio" will be automatically enhanced without adding additional data-role attributes.
(5) Check button, input element marked with type="checkbox" will be automatically enhanced without adding additional data-role attribute.
(6) Select list, select element will be automatically enhanced, no need to add additional data-role attribute.
(7) When you draw the bar, the input element marked with type="range" will be automatically enhanced without adding additional data-role attributes.
(8) Switch, after adding the data-role="slider" attribute to the select element, it will be enhanced to become the switch component of jQuery Mobile. There can only be two options in the select.
2. Component usage specificationsAll form components, as long as they need to transmit data to the server, should be wrapped in a form tag, and the action and method attributes of the form should be specified. Of course, if you are using local storage such as Web SQL Database, that is, the data does not need to be transmitted to the server, you can submit it without the form tag and sumbit. In addition, the form id needs to be unique in the entire site. Since jQuery Mobile uses Ajax navigation, different pages can be loaded into a DOM at the same time. Therefore, the form id must be unique in the entire site to ensure that the form id of each DOM is different.
Each form element should have a corresponding label. The for value of the label should be the same as the id of the element to make it semantically related, and you can use an attribute with data-role="fieldcontain" Wrapped in a p or fieldset container, jQuery Mobile will automatically add a thin border at the bottom of the container as a separation.
2. Detailed explanation of form components
1. Text input boxIn jQuery Mobile, text input boxes and text input fields are marked using standard HTML and support some HTML5 There are many input types, such as password, email, tel, number, range, and more. For some types (range, serach), jQuery Mobile will convert them to text input types and standardize their styles. The following is text input The calling code and diagram of the frame.
<p> <label>文本输入框</label> <input> </p>
2. Text input field
<p> <label>文本输入域</label> <textarea></textarea> </p>
3. Search input Box As mentioned above, there is a magnifying glass icon on the left side of the enhanced input box. Click to trigger the search. After entering the content, a cross icon will appear on the right side of the input box. Click to clear the entered content.
<p> <label>搜索输入框</label> <input> </p>
4.单选框单选框组件用于在页面中提供一组选项,并且只能选择其中一个选项。在 jQuery Mobile 中,单选框组件不但在外观上美化了,还增加了一些图标用于增强视觉反馈。 type="radio" 标记的 input 元素会自动增强为单选框组件,但 jQuery Mobile 建议开发者使用一个带 data-role="controlgroup" 属性的 fieldset 标签包括选项,并且在 fieldset 内增加一个 legend 元素,用于表示该单选框的标题。
<p> </p>
5.复选框复选框也是用于在页面中提供一组选项的,但可以同时选择多个选项。与单选框相同,复选框组件也无需额外调用 data-role 属性, type="checkbox" 标记的 input 元素会自动增强为 jQuery Mobile 样式,当然 jQuery Mobile 也建议开发者使用一个带 data-role="controlgroup" 属性的 fieldset 标签包括选项,并且在 fieldset 内增加一个 legend 元素,用于表示该复选框的标题。
<p> </p>
默认的复选框组件是垂直排列选项的,我们可以在 fieldset 上添加 data-type="horizontal" 使其样式改为水平按钮组的样式, jQuery Mobile 会使选项元素浮动并去掉图标。
6.选择列表选择列表是基于 HTML select 元素的,具有原生菜单和自定义菜单两种形式,原生菜单与自定义菜单都有一个样式美化了的选择按钮,不同的是在自定义菜单中原生的 option 元素将被隐藏, jQuery Mobile 会生成一个由 CSS3 和 HTML5 构成的菜单代替,并且这个菜单是 ARIA 的。
关于 ARIA ,这里小科普一下: ARIA, 即 Accessible Rich Internet Application ( 加强无障碍网页应用程序 ),它是 W3C 的无障碍网页倡议(WAI)工作小组在倡导大家使用的无障碍网页应用技术。它是一套独立的规范,可以帮助 Web 程序,尤其是使运用了大量前端技巧( Ajax )的网页更具备可访问性,通俗点说,读屏设备等不大能理解前端语义的设备将可以了解到页面的内容,这样残疾人士不仅可以浏览网页甚至与页面进行交互。
HTML select 元素会被自动增强为原生的选择列表组件,若需要自定义样式菜单,可以在 select 上添加 data-native-menu="false" 属性。
若需要禁止选择某一个选项,可以在选项的 option 标签上添加 disabled="disabled"。
下面给出一个自定义菜单的例子:
<p> <label>选择列表</label> <select> <option>12小时</option> <option>一天</option> <option>两天</option> <option>一周</option> </select> </p>
选择按钮
自定义菜单
若需要多项选择,则可以在 select 标签上添加 multiple="multiple" ,但原生的选择菜单不支持该功能。对于多项选择菜单,还有几点要注意的:
(1)jQuery Mobile 会在菜单里创建一个 jQuery Mobile header ,并在 header 的左边添加一个关闭菜单按钮。
(2)当用户选择两个或以上选项时选择按钮右侧会出现一个小图标,图标内显示已选择选项的个数。
(3)所有选择的选项文本会显示在选择按钮上,如果文字超出按钮长度,会以省略号代替多余的内容。
(4)如果选项过多,菜单会以新页面的形式显示。
多项选择列表实例
<p> <label>多项选择列表</label> <select> <option>12小时</option> <option>一天</option> <option>两天</option> <option>一周</option> </select> </p>
另外我们还可以对选项进行分组,使用以下标签标签把同一组的选项包裹起来,其中 label 的值为该分组的标题。
<optgroup></optgroup>
分组选择列表实例
<p> <label>分组选择列表</label> <select> <optgroup> <option>12小时</option> <option>一天</option> <option>两天</option> <option>一周</option> </optgroup> <optgroup> <option>一个月</option> <option>一季度</option> <option>一年</option> </optgroup> </select> </p>
7.滑杆在 jQuery Mobile 中,type="range" ( HTML5 属性值 ) 的 input 元素会被增强为划杆组件,该组件可以通过一些属性值配置,value 设置滑杆的初始值, min 和 max 分别设置滑杆的下限和上限。另外滑杆组件还支持键盘响应,键盘的右箭头,上箭头,Page Up 键都可以增加滑杆的当前值,相应的,左键头,下箭头, Page Down 键可以减少滑杆的当前值,使用 Home 键和 End 键则可以设置当前值为最小值(下限)和最大值(上限)。
<p> <label>滑杆</label> <input> </p>
8.开关具有两个 option 的 select 元素添加 data-role="slider" 属性后会被增强为 jQuery Mobile 的开关组件,用于表示布尔型数据( ture or false ),拖动滑动条可以在“开”与“关”之间选择,其中第一个 option 会被渲染成“开”的样式。
<p> <label>开关</label> <select> <option>关闭</option> <option>开启</option> </select> </p>
三.提交表单jQuery Mobile 自动采用 Ajax 的方式提交表单,默认的 method 为 get ,action 为当前页面的相对路径,在表单页面和结果页面之间会有平滑的转场过渡,并且可以在表单上使用 data-transition 指定转场效果。如果不想使用 Ajax 的方式提交表单,可以在全局事件禁用 Ajax (如何全局禁止 Ajax 并不在本文讨论范围,但会在本系列的后续文章中作详细说明)或是在 form 上添加 data-ajax="false" 属性,下面是 data-transition 的所有可取值。
slide 滑动(默认值,从左至右滑出), slideup(从下至上滑出), slidedown(从上至下滑出), pop(从中心渐显展开), fade(渐显), flip(翻转)。
四.主题样式关于主题样式,在上一文中已经介绍过了,这里引用一下:
“data-theme=“” 属性, 所有的 jQuery Mobile 组件均支持该属性,用于设置组件的颜色, 该属性默认有五个值 a, b, c, d, e,分别代表由深到浅五种颜色,另外开发者还可以通过在 CSS 里添加相应的 Class 来自定义颜色。”
五.刷新表单组件由于 jQuery Mobile 对原生的 HTML 表单元素进行了渲染和追加元素,所以开发者想通过 js 直接控制表单组件会比较麻烦(如使用了 Web SQL Database 储存了数据在本地,在打开网页时希望通过 js 获取数据并给表单组件赋值的情况),因此 jQuery Mobile 设计了用 js 给表单组件赋值的 API ,下面逐一举例介绍:
1.单选按钮选择第一个选项
$("input[type='radio']:first").attr("checked",true).checkboxradio("refresh");
2.复选按钮选择第一个选项
$("input[type='checkbox']:first").attr("checked",true).checkboxradio("refresh");
3.选择列表选择第一个选项,注意选项的索引是从1开始
var myselect = $('select#select-choice-1'); myselect[0].selectedIndex = 1; myselect.selectmenu('refresh');
4.滑杆设置值为40
$("input[type=range]").val(40).slider("refresh");
5.开关选择第一个选项
var myswitch = $('select#shakeToClear'); myswitch[0].selectedIndex = 1; myswitch.slider('refresh');
注: refresh 方法不能直接使用在 Ajax 获取的内容上,例如 js 要操纵的表单组件在另一个 Page, js 只在首页加载时载入,这样 jQuery Mobile 会出错(不会有直接报错,只是该段 js 会失效),我们可以使用 live('pagebeforeshow', function(){}) 方法使到该段 js 在过场到表单组件所在页面时才载入( pagebeforeshow 是 jQuery Mobile 事件之一,此外还有 pagebeforehide, pageshow, pagehide 三种方法)。下面给出一个例子:
$('#setting').live('pagebeforeshow', function(){ // 在页面显示时选择菜单中第一个选项 var myselect = $('select#select-choice-1'); myselect[0].selectedIndex = 1; myselect.selectmenu('refresh'); });
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of How to use form components in Mobile framework. For more information, please follow other related articles on the PHP Chinese website!

如何实现PHP表单提交后的页面跳转【简介】在Web开发中,表单的提交是一项常见的功能需求。当用户填写完表单并点击提交按钮后,通常需要将表单数据发送至服务器进行处理,并在处理完后将用户重定向至另一个页面。本文将介绍如何使用PHP来实现表单提交后的页面跳转。【步骤一:HTML表单】首先,我们需要在HTML页面中编写一个包含表单的页面,以便用户填写需要提交的数据。

如何处理PHP表单中的用户权限管理随着Web应用程序的不断发展,用户权限管理是一个重要的功能之一。用户权限管理可以控制用户在应用程序中的操作权限,保证数据的安全性和合法性。在PHP表单中,用户权限管理可以通过一些简单的代码来实现。本文将介绍如何处理PHP表单中的用户权限管理,并给出相应的代码示例。一、用户角色的定义和管理首先,对用户角色进行定义和管理是用户权

Yaber products are available on Amazon, and now the company offers a new projector - Yaber T2. The model is available in two versions: the Yaber T2 and the Yaber T2 Plus. The Yaber T2 Plus costs 349 dollars instead of 299 dollars and comes with a TV

PHP表单处理:表单数据查询与筛选引言在Web开发中,表单是一种重要的交互方式,用户可以通过表单向服务器提交数据并进行进一步的处理。本文将介绍如何使用PHP处理表单数据的查询与筛选功能。表单的设计与提交首先,我们需要设计一个包含查询与筛选功能的表单。常见的表单元素包括输入框、下拉列表、单选框、复选框等,根据具体需求进行设计。用户在提交表单时,会将数据以POS

Java实现表单的实时验证与提示功能随着网络应用的普及和发展,表单的使用也变得越来越重要。表单是网页中用于收集和提交用户数据的元素,例如注册或登录页面的表单。在用户填写表单时,经常需要对其输入的数据进行验证和提示,以保证数据的正确性和完整性。在本文中,我们将介绍如何使用Java语言实现表单的实时验证与提示功能。HTML表单的搭建首先,我们需要使用HTML语言

HeliumMobile是什么?HeliumMobile代币全面解读!HeliumMobile拥有通过独特的代币经济和合作伙伴关系在通信行业中创造显著影响的潜力。然而,项目的成功将依赖于其能否继续创新、扩大市场接受度并克服技术和监管挑战。今天本站小编给大家分享的是HeliumMobile的详细介绍,需要的朋友一起看看吧!HeliumMobile是什么HeliumMobile是一种建立在分散且加密驱动Helium网络上的一种无线电话服务。这项创新服务通过分散的Helium网络提供蜂窝连接,并得到T

如何处理PHP表单中的下拉列表选项下拉列表是Web表单中常用的元素,它允许用户从预先定义的选项中选择一个或多个值。在PHP中,我们可以通过一些简单的代码实现下拉列表的处理。本文将向你展示如何使用PHP来处理表单中的下拉列表选项。HTML代码中的下拉列表通常使用<select>和<option>标签来定义。<select>标

Nette框架是一款用于PHPWeb开发的轻量级框架,以其简单易用、高效稳定的特点受到了广泛的欢迎和使用。在开发Web应用时,使用表单和验证是不可避免的需求。本文将介绍如何在Nette框架中使用表单和验证。一、表单构建在Nette框架中,表单可以通过Form类来创建。Form类在NetteForms命名空间中,可以通过use关键字引入。useNetteF


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.