PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/docs/forms/docs-forms.html

https://github.com/Wilto/jquery-mobile
HTML | 238 lines | 181 code | 57 blank | 0 comment | 0 complexity | a9e32a1498db4a120360984f814cf75a MD5 | raw file
  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. <div data-role="page" class="type-interior">
  16. <div data-role="header" data-theme="f">
  17. <h1>Forms</h1>
  18. <a href="../../" data-icon="home" data-iconpos="notext" data-direction="reverse" class="ui-btn-right jqm-home">Home</a>
  19. </div><!-- /header -->
  20. <div data-role="content">
  21. <div class="content-primary">
  22. <p>jQuery Mobile provides a complete set of finger-friendly form elements that are based on native HTML form elements.</p>
  23. <h2>Form structure</h2>
  24. <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>
  25. <code>
  26. &lt;form action=&quot;form.php&quot; method=&quot;post&quot;&gt;
  27. ...
  28. &lt;/form&gt;
  29. </code>
  30. <h2>Markup conventions</h2>
  31. <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>
  32. <h2>Hiding labels accessibly</h2>
  33. <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 technologiesfor example, when letting an elements <code>placeholder</code> attribute serve as a labelapply the helper class <code>ui-hidden-accessible</code> to the label itself:</p>
  34. <code>
  35. <pre>
  36. &lt;label for="username" <strong>class="ui-hidden-accessible"</strong>>Username:&lt;/label&gt;
  37. &lt;input type="text" name="username" id="username" value="" placeholder="Username"/&gt;
  38. </pre>
  39. </code>
  40. <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>
  41. <code>
  42. <pre>
  43. &lt;div data-role="fieldcontain" <strong>class="ui-hide-label"</strong>&gt;
  44. &lt;label for="username">Username:&lt;/label&gt;
  45. &lt;input type="text" name="username" id="username" value="" placeholder="Username"/&gt;
  46. &lt;/div&gt;
  47. </pre>
  48. </code>
  49. <p>Both of the above examples will render as:</p>
  50. <div data-role="fieldcontain" class="ui-hide-label">
  51. <label for="username">Username:</label>
  52. <input type="text" name="username" id="username" value="" placeholder="Username" />
  53. </div>
  54. <p>While the label will no longer be visible, it will be available to assisitive technologies such as screen readers.</p>
  55. <h2>Disabling form elements</h2>
  56. <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. Note that you can disable form buttons, but not links with a role of button because links don't have a parallel disabled feature in HTML. If you need to disable a link-based button, it's possible to apply the same disabled class <code>ui-disabled</code> with JavaScript to the enhanced button markup generated to achieve this effect. </p>
  57. <div data-role="fieldcontain">
  58. <label for="foo">Text Input:</label>
  59. <input type="text" name="name" id="foo" value="" disabled />
  60. </div>
  61. <div data-role="fieldcontain">
  62. <fieldset data-role="controlgroup">
  63. <legend>Gender:</legend>
  64. <input type="radio" name="gender" id="radio-female" value="f" disabled />
  65. <label for="radio-female">Female</label>
  66. <input type="radio" name="gender" id="radio-male" value="m" disabled />
  67. <label for="radio-male">Male</label>
  68. </fieldset>
  69. </div>
  70. <div data-role="fieldcontain">
  71. <label for="flip-s">Server status:</label>
  72. <select name="slider" id="flip-s" data-role="slider" disabled >
  73. <option value="off">Off</option>
  74. <option value="on">On</option>
  75. </select>
  76. </div>
  77. <div data-role="fieldcontain">
  78. <label for="select-choice-x" class="select">Shipping:</label>
  79. <select name="select-shipper" id="select-choice-x" disabled>
  80. <option></option>
  81. <option value="standard">Standard</option>
  82. <option value="rush">Rush</option>
  83. <option value="express">Express</option>
  84. <option value="overnight">Overnight</option>
  85. </select>
  86. </div>
  87. <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>
  88. <h2>Field containers</h2>
  89. <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>
  90. <p>For example:</p>
  91. <pre><code>
  92. &lt;div data-role=&quot;fieldcontain&quot;&gt;
  93. &lt;label for="name">Text Input:&lt;/label&gt;
  94. &lt;input type="text" name="name" id="name" value="" /&gt;
  95. &lt;/div&gt;
  96. </code></pre>
  97. <p>Will render as:</p>
  98. <div data-role="fieldcontain">
  99. <label for="name">Text Input:</label>
  100. <input type="text" name="name" id="name" value="" />
  101. </div>
  102. <p>For additional examples, see the <a href="forms-all.html">form elements gallery</a></p>
  103. <h2>Auto-initialization of form elements</h2>
  104. <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>
  105. <h2>Initializing groups of dynamically-injected form elements</h2>
  106. <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>
  107. <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>
  108. <code>
  109. $( ...new markup that contains widgets... ).appendTo( ".ui-page" ).trigger( "create" );
  110. </code>
  111. <h2>Refreshing form elements</h2>
  112. <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>
  113. <h4>Checkboxes:</h4>
  114. <code>
  115. $("input[type='checkbox']").attr("checked",true).checkboxradio("refresh");
  116. </code>
  117. <h4>Radios:</h4>
  118. <code>
  119. $("input[type='radio']").attr("checked",true).checkboxradio("refresh");
  120. </code>
  121. <h4>Selects:</h4>
  122. <code><pre>
  123. var myselect = $("select#foo");
  124. myselect[0].selectedIndex = 3;
  125. myselect.selectmenu("refresh");
  126. </pre></code>
  127. <h4>Sliders:</h4>
  128. <code>
  129. $("input[type=range]").val(60).slider("refresh");
  130. </code>
  131. <h4>Flip switches (they use slider):</h4>
  132. <code><pre>
  133. var myswitch = $("select#bar");
  134. myswitch[0].selectedIndex = 1;
  135. myswitch.slider("refresh");
  136. </pre></code>
  137. <h2>Preventing auto-initialization of form elements</h2>
  138. <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>
  139. <pre><code>
  140. &lt;label for=&quot;foo&quot;&gt;
  141. &lt;select name=&quot;foo&quot; id=&quot;foo&quot; <strong> data-role=&quot;none&quot;</strong>&gt;
  142. &lt;option value="a" &gt;A&lt;/option&gt;
  143. &lt;option value="b" &gt;B&lt;/option&gt;
  144. &lt;option value="c" &gt;C&lt;/option&gt;
  145. &lt;/select&gt;
  146. </code></pre>
  147. <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>
  148. <pre><code>
  149. $(document).bind('mobileinit',function(){
  150. <strong>$.mobile.page.prototype.options.keepNative = "select, input.foo, textarea.bar";</strong>
  151. });
  152. </pre></code>
  153. <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>
  154. </div><!--/content-primary -->
  155. <div class="content-secondary">
  156. <div data-role="collapsible" data-collapsed="true" data-theme="b" data-content-theme="d">
  157. <h3>More in this section</h3>
  158. <ul data-role="listview" data-theme="c" data-dividertheme="d">
  159. <li data-role="list-divider">Form elements</li>
  160. <li data-theme="a"><a href="docs-forms.html">Form basics</a></li>
  161. <li><a href="forms-all.html">Form element gallery</a></li>
  162. <li><a href="textinputs/">Text inputs</a></li>
  163. <li><a href="search/">Search inputs</a></li>
  164. <li><a href="slider/">Slider</a></li>
  165. <li><a href="switch/">Flip toggle switch</a></li>
  166. <li><a href="radiobuttons/">Radio buttons</a></li>
  167. <li><a href="checkboxes/">Checkboxes</a></li>
  168. <li><a href="selects/">Select menus</a></li>
  169. <li><a href="forms-themes.html">Theming forms</a></li>
  170. <li><a href="forms-all-native.html">Native form elements</a></li>
  171. <li><a href="forms-sample.html">Submitting forms</a></li>
  172. </ul>
  173. </div>
  174. </div>
  175. </div><!-- /content -->
  176. <div data-role="footer" class="footer-docs" data-theme="c">
  177. <p>&copy; 2011 The jQuery Project</p>
  178. </div>
  179. </div><!-- /page -->
  180. </body>
  181. </html>