PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

HTML | 267 lines | 200 code | 67 blank | 0 comment | 0 complexity | f12caba083da46e6465af4b95ab831ed 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="../../../../compiled/" />
  8. <link rel="stylesheet" href="../_assets/css/jqm-docs.css"/>
  9. <script src="../../js/jquery.js"></script>
  10. <script src="../../docs/_assets/js/jqm-docs.js"></script>
  11. <script src="../../js/"></script>
  12. </head>
  13. <body>
  14. <div data-role="page" class="type-interior">
  15. <div data-role="header" data-theme="f">
  16. <h1>Forms</h1>
  17. <a href="../../" data-icon="home" data-iconpos="notext" data-direction="reverse">Home</a>
  18. <a href="../nav.html" data-icon="search" data-iconpos="notext" data-rel="dialog" data-transition="fade">Search</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 ordinary HTTP POST or GET still apply. Additionally, 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. You must be careful to use unique <code>id</code> attributes so there will be only one of each in the DOM. Be sure to pair them properly with <code>label</code> elements via the <code>for</code> attribute.</p>
  32. <h2>Mini sized elements</h2>
  33. <p>For a more compact version of all form elements and buttons, add the <code>data-mini="true"</code> attribute to the element to create a <a href="forms-all-mini.html">mini version</a>. This is useful in toolbars and tight spaces but is still finger-friendly. It's possible to add this attribute to a fieldcontainer to set this on a number of elements at once.</p>
  34. <pre><code>
  35. &lt;label for=&quot;basic&quot;&gt;Text Input:&lt;/label&gt;
  36. &lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;basic&quot; <strong>data-mini=&quot;true&quot;</strong> /&gt;
  37. </code></pre>
  38. <p>This will produce an input that is not as tall as the standard version and has a smaller text size.</p>
  39. <label for="mini">Text Input:</label>
  40. <input type="text" name="name" id="mini" value="" data-mini="true" />
  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 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>
  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. <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>
  50. <code>
  51. <pre>
  52. &lt;div data-role="fieldcontain" <strong>class="ui-hide-label"</strong>&gt;
  53. &lt;label for="username"&gt;Username:&lt;/label&gt;
  54. &lt;input type="text" name="username" id="username" value="" placeholder="Username"/&gt;
  55. &lt;/div&gt;
  56. </pre>
  57. </code>
  58. <p>Both of the above examples will render as:</p>
  59. <div data-role="fieldcontain" class="ui-hide-label">
  60. <label for="username">Username:</label>
  61. <input type="text" name="username" id="username" value="" placeholder="Username" />
  62. </div>
  63. <p>While the label will no longer be visible, it will be available to assisitive technologies such as screen readers.</p>
  64. <h2>Disabling form elements</h2>
  65. <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>
  66. <div data-role="fieldcontain">
  67. <label for="foo">Text Input:</label>
  68. <input type="text" name="name" id="foo" value="" disabled />
  69. </div>
  70. <div data-role="fieldcontain">
  71. <fieldset data-role="controlgroup">
  72. <legend>Gender:</legend>
  73. <input type="radio" name="gender" id="radio-female" value="f" disabled />
  74. <label for="radio-female">Female</label>
  75. <input type="radio" name="gender" id="radio-male" value="m" disabled />
  76. <label for="radio-male">Male</label>
  77. </fieldset>
  78. </div>
  79. <div data-role="fieldcontain">
  80. <label for="flip-s">Server status:</label>
  81. <select name="flip-s" id="flip-s" data-role="slider" disabled >
  82. <option value="off">Off</option>
  83. <option value="on">On</option>
  84. </select>
  85. </div>
  86. <div data-role="fieldcontain">
  87. <label for="slider">Max bandwidth:</label>
  88. <input type="range" name="slider" id="slider" value="0" min="0" max="100" disabled />
  89. </div>
  90. <div data-role="fieldcontain">
  91. <label for="select-choice-x" class="select">Shipping:</label>
  92. <select name="select-shipper" id="select-choice-x" disabled>
  93. <option></option>
  94. <option value="standard">Standard</option>
  95. <option value="rush">Rush</option>
  96. <option value="express">Express</option>
  97. <option value="overnight">Overnight</option>
  98. </select>
  99. </div>
  100. <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>
  101. <h2>Field containers</h2>
  102. <p>To improve the styling of labels and form elements on wider screens, wrap 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>
  103. <h2>Forms in toolbars</h2>
  104. <p>While all form elements are now tested to work correctly within <em>static</em> toolbars as of jQuery Mobile 1.1, we recommend extensive testing when using form elements within <em>fixed</em> toolbars or within any <code>position: fixed</code> elements. This can potentially trigger a number of unpredictable issues in various mobile browsers, Android 2.2/2.3 in particular (detailed in <a href="">Known issues in Android 2.2/2.3</a>).</p>
  105. <p>For example:</p>
  106. <pre><code>
  107. &lt;div data-role=&quot;fieldcontain&quot;&gt;
  108. &lt;label for="name"&gt;Text Input:&lt;/label&gt;
  109. &lt;input type="text" name="name" id="name" value="" /&gt;
  110. &lt;/div&gt;
  111. </code></pre>
  112. <p>Will render as:</p>
  113. <div data-role="fieldcontain">
  114. <label for="name">Text Input:</label>
  115. <input type="text" name="name" id="name" value="" />
  116. </div>
  117. <p>For additional examples, see the <a href="forms-all.html">form elements gallery</a></p>
  118. <h2>Auto-initialization of form elements</h2>
  119. <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. 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>
  120. <h2>Initializing groups of dynamically-injected form elements</h2>
  121. <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>
  122. <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>
  123. <code>
  124. $( markup that contains widgets... ).appendTo( ".ui-page" ).trigger( "create" );
  125. </code>
  126. <h2>Refreshing form elements</h2>
  127. <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>
  128. <h4>Checkboxes:</h4>
  129. <code>
  130. $("input[type='checkbox']").prop("checked",true).checkboxradio("refresh");
  131. </code>
  132. <h4>Radios:</h4>
  133. <code>
  134. $("input[type='radio']").prop("checked",true).checkboxradio("refresh");
  135. </code>
  136. <h4>Selects:</h4>
  137. <code><pre>
  138. var myselect = $("#selectfoo");
  139. myselect[0].selectedIndex = 3;
  140. myselect.selectmenu("refresh");
  141. </pre></code>
  142. <h4>Sliders:</h4>
  143. <code>
  144. $("input[type='range']").val(60).slider("refresh");
  145. </code>
  146. <h4>Flip switches (they use slider):</h4>
  147. <code><pre>
  148. var myswitch = $("#selectbar");
  149. myswitch[0].selectedIndex = 1;
  150. myswitch.slider("refresh");
  151. </pre></code>
  152. <h2>Preventing auto-initialization of form elements</h2>
  153. <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>
  154. <pre><code>
  155. &lt;label for=&quot;foo&quot;&gt;
  156. &lt;select name=&quot;foo&quot; id=&quot;foo&quot; <strong>data-role=&quot;none&quot;</strong>&gt;
  157. &lt;option value="a"&gt;A&lt;/option&gt;
  158. &lt;option value="b"&gt;B&lt;/option&gt;
  159. &lt;option value="c"&gt;C&lt;/option&gt;
  160. &lt;/select&gt;
  161. </code></pre>
  162. <p>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>
  163. <pre><code>
  164. $(document).bind('mobileinit',function(){
  165. <strong>$ = "select,,";</strong>
  166. });
  167. </code></pre>
  168. <p>Alternately you can use the <code>data-enhance="false"</code> data attribute on a parent element with <code>$.mobile.ignoreContentEnabled</code> set to true. Beware though, this will incur a performance penalty for each and every element in the page that would otherwise be enhanced as jQuery Mobile must traverse the set of parents to look for those elements.</p>
  169. <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 and appearance of the menu itself and benefit from the visual augmentation of the select button by jQuery Mobile, you can set <code>$.mobile.selectmenu.prototype.options.nativeMenu</code> to true in a <code>mobileinit</code> callback as a global setting or use <code>data-native-menu="true"</code> on a case by case basis.</p>
  170. <h2>File Inputs</h2>
  171. <p>Using a multipart form with a file input is not supported by ajax. In this case you should decorate the parent form with <code>data-ajax="false"</code> to ensure the form is submitted properly to the server.</p>
  172. </div><!--/content-primary -->
  173. <div class="content-secondary">
  174. <div data-role="collapsible" data-collapsed="true" data-theme="b" data-content-theme="d">
  175. <h3>More in this section</h3>
  176. <ul data-role="listview" data-theme="c" data-dividertheme="d">
  177. <li data-role="list-divider">Form elements</li>
  178. <li data-theme="a"><a href="docs-forms.html">Form basics</a></li>
  179. <li><a href="forms-all.html">Form element gallery</a></li>
  180. <li><a href="forms-all-mini.html">Mini form element gallery</a></li>
  181. <li><a href="textinputs/">Text inputs</a></li>
  182. <li><a href="search/">Search inputs</a></li>
  183. <li><a href="slider/">Slider</a></li>
  184. <li><a href="switch/">Flip toggle switch</a></li>
  185. <li><a href="radiobuttons/">Radio buttons</a></li>
  186. <li><a href="checkboxes/">Checkboxes</a></li>
  187. <li><a href="selects/">Select menus</a></li>
  188. <li><a href="forms-themes.html">Theming forms</a></li>
  189. <li><a href="forms-all-native.html">Native form elements</a></li>
  190. <li><a href="forms-sample.html">Submitting forms</a></li>
  191. </ul>
  192. </div>
  193. </div>
  194. </div><!-- /content -->
  195. <div data-role="footer" class="footer-docs" data-theme="c">
  196. <p>&copy; 2012 jQuery Foundation and other contributors</p>
  197. </div>
  198. </div><!-- /page -->
  199. </body>
  200. </html>