index.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 - Radio Buttons</title>
  7. <link rel="stylesheet" href="../../../jquery.mobile-1.0.1.min.css" />
  8. <link rel="stylesheet" href="../../_assets/css/jqm-docs.css"/>
  9. <script src="../../../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="../../../jquery.mobile-1.0.1.min.js"></script>
  13. </head>
  14. <body>
  15. <div data-role="page" class="type-interior">
  16. <div data-role="header" data-theme="f">
  17. <h1>Radio buttons</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. <form action="#" method="get">
  23. <h2>Radio buttons</h2>
  24. <ul data-role="controlgroup" data-type="horizontal" class="localnav">
  25. <li><a href="index.html" data-role="button" data-transition="fade" class="ui-btn-active">Basics</a></li>
  26. <li><a href="options.html" data-role="button" data-transition="fade">Options</a></li>
  27. <li><a href="methods.html" data-role="button" data-transition="fade">Methods</a></li>
  28. <li><a href="events.html" data-role="button" data-transition="fade">Events</a></li>
  29. </ul>
  30. <p>Radio buttons are used to provide a list of options where only a single items can be selected. Traditional desktop radio buttons are not optimized for touch input so in jQuery Mobile, we style the <code>label</code> for the radio buttons so they are larger and look clickable. A custom set of icons are added to the label to provide additional visual feedback.</p>
  31. <p>Both the radio and checkbox controls below use standard input/label markup, but are styled to be more touch-friendly. The styled control you see is actually the label element, which sits over the real input, so if images fail to load, you'll still have a functional control. In most browsers, clicking the label automatically triggers a click on the input, but we've had to trigger the update manually for a few mobile browsers that don't do this natively. On the desktop, these controls are keyboard and screen-reader accessible. View the <a href="../../api/data-attributes.html">data- attribute reference</a> to see all the possible attributes you can add to radio buttons.</p>
  32. <h2>Vertically grouped radio buttons</h2>
  33. <p>To create a set of radio buttons, add an <code>input</code> with a <code>type="radio"</code> attribute and a corresponding <code>label</code>. Set the <code>for</code> attribute of the <code>label</code> to match the ID of the <code>input</code> so they are semantically associated.</p>
  34. <p>Because radio buttons use the <code>label</code> element for the text displayed next to the checkbox form element, we recommend wrapping the radio buttons in a <code>fieldset</code> element that has a <code>legend</code> which acts as the title for the question.</p>
  35. <p>To visually integrate multiple radio buttons into a vertically grouped button set, the framework will automatically remove all margins between buttons and round only the top and bottom corners of the set if there is a <code> data-role="controlgroup"</code> attribute on the container.</p>
  36. <pre><code>
  37. <strong>&lt;fieldset data-role=&quot;controlgroup&quot;&gt;
  38. &lt;legend&gt;Choose a pet:&lt;/legend&gt;</strong>
  39. &lt;input type=&quot;radio&quot; name=&quot;radio-choice-1&quot; id=&quot;radio-choice-1&quot; value=&quot;choice-1&quot; checked=&quot;checked&quot; /&gt;
  40. &lt;label for=&quot;radio-choice-1&quot;&gt;Cat&lt;/label&gt;
  41. &lt;input type=&quot;radio&quot; name=&quot;radio-choice-1&quot; id=&quot;radio-choice-2&quot; value=&quot;choice-2&quot; /&gt;
  42. &lt;label for=&quot;radio-choice-2&quot;&gt;Dog&lt;/label&gt;
  43. &lt;input type=&quot;radio&quot; name=&quot;radio-choice-1&quot; id=&quot;radio-choice-3&quot; value=&quot;choice-3&quot; /&gt;
  44. &lt;label for=&quot;radio-choice-3&quot;&gt;Hamster&lt;/label&gt;
  45. &lt;input type=&quot;radio&quot; name=&quot;radio-choice-1&quot; id=&quot;radio-choice-4&quot; value=&quot;choice-4&quot; /&gt;
  46. &lt;label for=&quot;radio-choice-4&quot;&gt;Lizard&lt;/label&gt;
  47. <strong>&lt;/fieldset&gt;</strong>
  48. </code></pre>
  49. <p>This will produce a vertically grouped radio button set. The default styles set the width of the button group to 100% of the parent container and stacks the label on a separate line.</p>
  50. <fieldset data-role="controlgroup">
  51. <legend>Choose a pet:</legend>
  52. <input type="radio" name="radio-pet-1a" id="radio-pet-1a" value="choice-1" checked="checked" />
  53. <label for="radio-pet-1a">Cat</label>
  54. <input type="radio" name="radio-pet-1a" id="radio-pet-2a" value="choice-2" />
  55. <label for="radio-pet-2a">Dog</label>
  56. <input type="radio" name="radio-pet-1a" id="radio-pet-3a" value="choice-3" />
  57. <label for="radio-pet-3a">Hamster</label>
  58. <input type="radio" name="radio-pet-1a" id="radio-pet-4a" value="choice-4" />
  59. <label for="radio-pet-4a">Lizard</label>
  60. </fieldset>
  61. <pre><code>
  62. <strong>&lt;div data-role=&quot;fieldcontain&quot;&gt;
  63. </strong> &lt;fieldset data-role=&quot;controlgroup&quot;&gt;
  64. &lt;legend&gt;Choose a pet:&lt;/legend&gt;
  65. &lt;input type=&quot;radio&quot; name=&quot;radio-choice-1&quot; id=&quot;radio-choice-1&quot; value=&quot;choice-1&quot; checked=&quot;checked&quot; /&gt;
  66. &lt;label for=&quot;radio-choice-1&quot;&gt;Cat&lt;/label&gt;
  67. &lt;input type=&quot;radio&quot; name=&quot;radio-choice-1&quot; id=&quot;radio-choice-2&quot; value=&quot;choice-2&quot; /&gt;
  68. &lt;label for=&quot;radio-choice-2&quot;&gt;Dog&lt;/label&gt;
  69. &lt;input type=&quot;radio&quot; name=&quot;radio-choice-1&quot; id=&quot;radio-choice-3&quot; value=&quot;choice-3&quot; /&gt;
  70. &lt;label for=&quot;radio-choice-3&quot;&gt;Hamster&lt;/label&gt;
  71. &lt;input type=&quot;radio&quot; name=&quot;radio-choice-1&quot; id=&quot;radio-choice-4&quot; value=&quot;choice-4&quot; /&gt;
  72. &lt;label for=&quot;radio-choice-4&quot;&gt;Lizard&lt;/label&gt;
  73. &lt;/fieldset&gt;
  74. <strong>&lt;/div&gt;
  75. </strong> </code></pre>
  76. <p>To visually integrate multiple radio buttons into a vertically grouped button set, the framework will automatically remove all margins between buttons and round only the top and bottom corners of the set if there is a <code> data-role="controlgroup"</code> attribute on the container.</p>
  77. <div data-role="fieldcontain">
  78. <fieldset data-role="controlgroup">
  79. <legend>Choose a pet:</legend>
  80. <input type="radio" name="radio-pet-1" id="radio-pet-1" value="choice-1" checked="checked" />
  81. <label for="radio-pet-1">Cat</label>
  82. <input type="radio" name="radio-pet-1" id="radio-pet-2" value="choice-2" />
  83. <label for="radio-pet-2">Dog</label>
  84. <input type="radio" name="radio-pet-1" id="radio-pet-3" value="choice-3" />
  85. <label for="radio-pet-3">Hamster</label>
  86. <input type="radio" name="radio-pet-1" id="radio-pet-4" value="choice-4" />
  87. <label for="radio-pet-4">Lizard</label>
  88. </fieldset>
  89. </div>
  90. <h2>Horizontal radio button sets</h2>
  91. <p>Radio buttons can also be used for grouped button sets only a single button can be selected at once, such as a view switcher control. To make a horizontal radio button set, add the <code> data-type="horizontal"</code> to the <code>fieldset</code>.</p>
  92. <code>
  93. &lt;fieldset data-role=&quot;controlgroup&quot; <strong>data-type=&quot;horizontal&quot;</strong> &gt;
  94. </code>
  95. <div data-role="fieldcontain">
  96. <fieldset data-role="controlgroup" data-type="horizontal">
  97. <legend>Layout view:</legend>
  98. <input type="radio" name="radio-view" id="radio-view-a" value="list" />
  99. <label for="radio-view-a">List</label>
  100. <input type="radio" name="radio-view" id="radio-view-b" value="grid" />
  101. <label for="radio-view-b">Grid</label>
  102. <input type="radio" name="radio-view" id="radio-view-c" value="gallery" />
  103. <label for="radio-view-c">Gallery</label>
  104. </fieldset>
  105. </div>
  106. <p>The framework will float the labels so they sit side-by-side on a line, hides the radio button icons and only round the left and right edges of the group.</p>
  107. </form>
  108. </div><!--/content-primary -->
  109. <div class="content-secondary">
  110. <div data-role="collapsible" data-collapsed="true" data-theme="b" data-content-theme="d">
  111. <h3>More in this section</h3>
  112. <ul data-role="listview" data-theme="c" data-dividertheme="d">
  113. <li data-role="list-divider">Form elements</li>
  114. <li><a href="../docs-forms.html">Form basics</a></li>
  115. <li><a href="../forms-all.html">Form element gallery</a></li>
  116. <li><a href="../textinputs/index.html">Text inputs</a></li>
  117. <li><a href="../search/">Search input</a></li>
  118. <li><a href="../slider/">Slider</a></li>
  119. <li><a href="../switch/">Flip toggle switch</a></li>
  120. <li data-theme="a"><a href="index.html">Radio buttons</a></li>
  121. <li><a href="../checkboxes/">Checkboxes</a></li>
  122. <li><a href="../selects/">Select menus</a></li>
  123. <li><a href="../forms-themes.html">Theming forms</a></li>
  124. <li><a href="../forms-all-native.html">Native form elements</a></li>
  125. <li><a href="../forms-sample.html">Submitting forms</a></li>
  126. </ul>
  127. </div>
  128. </div>
  129. </div><!-- /content -->
  130. <div data-role="footer" class="footer-docs" data-theme="c">
  131. <p>&copy; 2011-2012 The jQuery Project</p>
  132. </div>
  133. </div><!-- /page -->
  134. </body>
  135. </html>