460b4b8a4b9ce9656526cfb4901bdf9804582135
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.0.1pre / docs / forms / docs-forms.html
1 <!DOCTYPE html>
2 <html>
3         <head>
4         <meta charset="utf-8">
5         <meta name="viewport" content="width=device-width, initial-scale=1">
6         <title>jQuery Mobile Docs - Forms</title>
7         <link rel="stylesheet"  href="../../css/themes/default/" />
8         <link rel="stylesheet" href="../_assets/css/jqm-docs.css"/>
9         <script src="../../js/jquery.js"></script>
10         <script src="../../experiments/themeswitcher/jquery.mobile.themeswitcher.js"></script>
11         <script src="../_assets/js/jqm-docs.js"></script>
12         <script src="../../js/"></script>
13 </head>
14 <body>
15
16         <div data-role="page" class="type-interior">
17
18                 <div data-role="header" data-theme="f">
19                 <h1>Forms</h1>
20                 <a href="../../" data-icon="home" data-iconpos="notext" data-direction="reverse" class="ui-btn-right jqm-home">Home</a>
21         </div><!-- /header -->
22
23         <div data-role="content">
24                 <div class="content-primary">
25                         <p>jQuery Mobile provides a complete set of finger-friendly form elements that are based on native HTML form elements.</p>
26
27                         <h2>Form structure</h2>
28
29                         <p>All forms should be wrapped in a <code>form</code> tag that has an <code>action</code> and <code>method</code> that will handle the form data processing on the server.</p>
30
31 <code>
32 &lt;form action=&quot;form.php&quot; method=&quot;post&quot;&gt;
33 ...
34 &lt;/form&gt;
35 </code>
36
37
38 <h2>Markup conventions</h2>
39 <p>When constructing forms to be used in jQuery Mobile, most of the standard guidelines used to create forms that submit via normal HTTP post or get still apply. However, one thing to keep in mind is that the <code>id</code> attributes of form controls need to be not only unique on a given page, but also unique across the pages in a site. This is because jQuery Mobile's single-page navigation model allows many different "pages" to be present in the DOM at the same time, so you must be careful to use unique <code>id</code> attributes so there will be only one of each in the DOM (and of course, be sure to pair them properly with <code>label</code> elements via the <code>for</code> attribute).</p>
40
41         <h2>Hiding labels accessibly</h2>
42         <p>For the sake of accessibility, jQuery Mobile requires that all form elements be paired with a meaningful <code>label</code>. To hide labels in a way that leaves them visible to assistive technologies—for example, when letting an element’s <code>placeholder</code> attribute serve as a label—apply the helper class <code>ui-hidden-accessible</code> to the label itself:</p>
43 <code>
44 <pre>
45 &lt;label for="username" <strong>class="ui-hidden-accessible"</strong>&gt;Username:&lt;/label&gt;
46 &lt;input type="text" name="username" id="username" value="" placeholder="Username"/&gt;
47 </pre>
48 </code>
49
50         <p>To hide labels within a field container and adjust the layout accordingly, add the class <code>ui-hide-label</code> to the field container as in the following:</p>
51
52 <code>
53 <pre>
54 &lt;div data-role="fieldcontain" <strong>class="ui-hide-label"</strong>&gt;
55         &lt;label for="username"&gt;Username:&lt;/label&gt;
56         &lt;input type="text" name="username" id="username" value="" placeholder="Username"/&gt;
57 &lt;/div&gt;
58 </pre>
59 </code>
60
61         <p>Both of the above examples will render as:</p>
62         <div data-role="fieldcontain" class="ui-hide-label">
63                 <label for="username">Username:</label>
64                 <input type="text" name="username" id="username" value="" placeholder="Username" />
65         </div>
66
67         <p>While the label will no longer be visible, it will be available to assisitive technologies such as screen readers.</p>
68
69
70         <h2>Disabling form elements</h2>
71         <p>All jQuery Mobile widgets can be disabled in the markup by adding the standard <code>disabled</code> attribute to the element, just like you would with native controls. Each form widget also has standard <code>disable</code> and <code>enable</code> methods that are documented with each form widget. Here are a few examples of disabled widgets: </p>
72         
73         <div data-role="fieldcontain">
74      <label for="foo">Text Input:</label>
75      <input type="text" name="name" id="foo" value="" disabled />
76         </div>
77         
78         <div data-role="fieldcontain">
79         <fieldset data-role="controlgroup">
80         <legend>Gender:</legend>
81                 <input type="radio" name="gender" id="radio-female" value="f" disabled />
82                 <label for="radio-female">Female</label>
83
84                 <input type="radio" name="gender" id="radio-male" value="m" disabled  />
85                 <label for="radio-male">Male</label>
86     </fieldset>
87     </div>
88
89         <div data-role="fieldcontain">
90         <label for="flip-s">Server status:</label>
91         <select name="flip-s" id="flip-s" data-role="slider" disabled >
92                 <option value="off">Off</option>
93                 <option value="on">On</option>
94         </select>
95     </div>    
96
97     <div data-role="fieldcontain">
98         <label for="slider">Max bandwidth:</label>
99         <input type="range" name="slider" id="slider" value="0" min="0" max="100" disabled />
100     </div>
101     
102         <div data-role="fieldcontain">
103                 <label for="select-choice-x" class="select">Shipping:</label>
104                 <select name="select-shipper" id="select-choice-x" disabled>
105                         <option></option>
106                         <option value="standard">Standard</option>
107                         <option value="rush">Rush</option>
108                         <option value="express">Express</option>
109                         <option value="overnight">Overnight</option>
110                 </select>
111         </div> 
112         
113         <p>Note that you can disable buttons created from <code>button</code> or <code>input</code>-based markup, but not links with a role of button. Links don't have a parallel disabled feature in HTML, but if you need to disable a link-based button (or any element), it's possible to apply the disabled class <code>ui-disabled</code> yourself with JavaScript to achieve the same effect. </p>
114         
115                 <h2>Field containers</h2>
116                 <p>To improve the styling to labels and form elements on wider screens, we recommend wrapping a <code>div</code> or <code>fieldset </code>with the <code> data-role="fieldcontain"</code> attribute around each label/form element. This framework aligns the input and associated label side-by-side, and breaks to stacked block-level elements below ~480px. The framework will also add a thin bottom border to act as a field separator.</p>
117
118                 <p>For example:</p>
119 <pre><code>
120 &lt;div data-role=&quot;fieldcontain&quot;&gt;
121 &lt;label for="name"&gt;Text Input:&lt;/label&gt;
122 &lt;input type="text" name="name" id="name" value="" /&gt;
123 &lt;/div&gt;
124 </code></pre>
125
126                 <p>Will render as:</p>
127
128                 <div data-role="fieldcontain">
129                         <label for="name">Text Input:</label>
130                         <input type="text" name="name" id="name" value=""  />
131                 </div>
132
133                 <p>For additional examples, see the <a href="forms-all.html">form elements gallery</a></p>
134                 
135                 
136         <h2>Auto-initialization of form elements</h2>
137         <p>By default, jQuery Mobile will automatically enhance certain native form controls into rich touch-friendly components. This is handled internally by finding form elements by tag name and running a plugin method on them, so for instance, a <code>select</code> element will be found and initialized with the "selectmenu" plugin, while an <code>input</code> element with a <code>type="checkbox"</code> will be enhanced with the "checkboxradio" plugin. Once initialized, you can address these enhanced components programmatically through their jQuery UI widget API methods - see options, methods and events listed on each form plugin's documentation page for details. </p>
138
139         <h2>Initializing groups of dynamically-injected form elements</h2> 
140         <p>If you should generate new markup client-side or load in content via AJAX and inject it into a page, you can trigger the <code>create</code> event to handle the auto-initialization for all the plugins contained within the new markup. This can be triggered on any element (even the page div itself), saving you the task of manually initializing each plugin (see below).</p> 
141
142         <p>For example, if a block of HTML markup (say a login form) was loaded in through Ajax, trigger the create event to automatically transform all the widgets it contains (inputs and buttons in this case) into the enhanced versions. The code for this scenario would be:</p>
143
144         <code>
145         $( ...new markup that contains widgets... ).appendTo( ".ui-page" ).trigger( "create" );
146         </code>
147
148         <h2>Refreshing form elements</h2>
149
150     <p>In jQuery Mobile, some enhanced form controls are simply styled (inputs), but others are custom controls (selects, sliders) built from, and kept in sync with, the native control. To programmatically update a form control with JavaScript, first manipulate the native control, then use the <code>refresh</code> method to tell the enhanced control to update itself to match the new state. Here are some examples of how to update common form controls, then call the <code>refresh</code> method:</p>
151         <h4>Checkboxes:</h4>
152
153 <code>
154 $("input[type='checkbox']").prop("checked",true).checkboxradio("refresh");
155 </code>
156
157 <h4>Radios:</h4>
158 <code>
159 $("input[type='radio']").prop("checked",true).checkboxradio("refresh");
160 </code>
161
162 <h4>Selects:</h4>
163 <code><pre>
164 var myselect = $("#selectfoo");
165 myselect[0].selectedIndex = 3;
166 myselect.selectmenu("refresh");
167 </pre></code>
168
169 <h4>Sliders:</h4>
170 <code>
171 $("input[type='range']").val(60).slider("refresh");
172 </code>
173
174 <h4>Flip switches (they use slider):</h4>
175
176 <code><pre>
177 var myswitch = $("#selectbar");
178 myswitch[0].selectedIndex = 1;
179 myswitch.slider("refresh");
180 </pre></code>
181
182         <h2>Preventing auto-initialization of form elements</h2>
183         <p>If you'd prefer that a particular form control be left untouched by jQuery Mobile, simply give that element the attribute <code> data-role="none"</code>. For example:</p>
184         <pre><code>
185 &lt;label for=&quot;foo&quot;&gt;
186 &lt;select name=&quot;foo&quot; id=&quot;foo&quot; <strong> data-role=&quot;none&quot;</strong>&gt;
187         &lt;option value="a" &gt;A&lt;/option&gt;
188         &lt;option value="b" &gt;B&lt;/option&gt;
189         &lt;option value="c" &gt;C&lt;/option&gt;
190 &lt;/select&gt;
191 </code></pre>
192
193
194         <p>Or, if you'd like to prevent auto-initialization without adding attributes to your markup, you can customize the selector that is used for preventing auto-initialization by setting the page plugin's <code>keepNative</code> option (which defaults to <code>[data-role="none"]</code>. Be sure to configure this option inside an event handler bound to the <code>mobileinit</code> event, so that it applies to the first page as well as subsequent pages that are loaded.</p>
195                 <pre><code>
196 $(document).bind('mobileinit',function(){
197         <strong>$.mobile.page.prototype.options.keepNative = "select, input.foo, textarea.bar";</strong>
198 });
199                 </pre></code>
200
201 <p>One special case is that of selects. The above sample will prevent any and all augmentation from taking place on select elements in the page if <code>select</code> is included. If you wish to retain the native performance, look/feel of the menu itself and benefit from the visual augmentation of the select button by jQuery Mobile you can set $.mobile.nativeSelectMenu to true in a <code>mobileinit</code> callback as a global setting or use <code>data-native="true"</code> on a case by case basis.</p>
202
203                 
204
205         </div><!--/content-primary -->          
206         
207         <div class="content-secondary">
208                 
209                 <div data-role="collapsible" data-collapsed="true" data-theme="b" data-content-theme="d">
210                         
211                                 <h3>More in this section</h3>
212                                 
213                                 <ul data-role="listview" data-theme="c" data-dividertheme="d">
214                                 
215                                         <li data-role="list-divider">Form elements</li>
216                                         <li data-theme="a"><a href="docs-forms.html">Form basics</a></li>
217                                         <li><a href="forms-all.html">Form element gallery</a></li>
218                                         <li><a href="textinputs/">Text inputs</a></li>
219                                         <li><a href="search/">Search inputs</a></li>
220                                         <li><a href="slider/">Slider</a></li>
221                                         <li><a href="switch/">Flip toggle switch</a></li>
222                                         <li><a href="radiobuttons/">Radio buttons</a></li>
223                                         <li><a href="checkboxes/">Checkboxes</a></li>
224                                         <li><a href="selects/">Select menus</a></li>
225                                         <li><a href="forms-themes.html">Theming forms</a></li>
226                                         <li><a href="forms-all-native.html">Native form elements</a></li>
227                                         <li><a href="forms-sample.html">Submitting forms</a></li>
228                                         
229         
230                                 </ul>
231                 </div>
232         </div>          
233
234 </div><!-- /content -->
235
236 <div data-role="footer" class="footer-docs" data-theme="c">
237                 <p>&copy; 2011 The jQuery Project</p>
238 </div>
239         
240 </div><!-- /page -->
241
242 </body>
243 </html>