📜  HTML 中的单选按钮与复选框

📅  最后修改于: 2021-10-28 03:13:38             🧑  作者: Mango

单选按钮:一般用在 HTML 表单中。当您需要从网站访问者那里收集一些数据时,需要 HTML 表单。当您只想从多个可用选项中选择一个选项时,可以使用单选按钮。

例子:


  

    
        Radio Button
    <title>
</head>
  
<body>
    <form>
        Do you agree this statement?
        <br>
        <input type="radio"
               name="agree" 
               value="yes">Yes
        <br>
        <input type="radio"
               name="agree" 
               value="no">No
        <br>
        <input type="Submit">
    </form>
</body>
  
</html>
</code></pre>
</div>
<p><strong>输出:</strong> <br/><img alt="" class="img-fluid" src="https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/Radio_Button_Vs_Checkbox_in_HTML_0.jpg"/></p>
<p><b>复选框:</b>复选框也主要用于 HTML 表单中。复选框允许您从选项列表中选择一个或多个选项。</p>
<p><b>例子:</b></p>
<p></p>
<div class="_ap_apex_ad" id="d6f45c53-ff16-4cc5-8b91-eb6ee7037116" style="min-height:280px"></div>
<p></p>
<div class="hcb_wrap">
<pre class="prism line-numbers lang-html" data-lang="HTML"><code class="replace"><html>
  
<head>
    <title>
        HTML Checkbox
    <title>
</head>
  
<body>
    <form>
        Choose languages you know:
        <br>
        <input type="checkbox" 
               name="C" 
               value="yes">C
        <br>
        <input type="checkbox" 
               name="C++" 
               value="yes">C++
        <br>
        <input type="checkbox" 
               name="Java" 
               value="yes">Java
        <br>
        <input type="checkbox" 
               name="Python" 
               value="yes">Python
        <br>
        <input type="Submit">
    </form>
</body>
  
</html>
</code></pre>
</div>
<p><strong>输出:</strong> <br/><img alt="" class="img-fluid" src="https://mangodoc.oss-cn-beijing.aliyuncs.com/geek8geeks/Radio_Button_Vs_Checkbox_in_HTML_1.jpg"/></p>
<p><b>单选按钮和复选框的区别</b></p>
<table>
<tr>
<th>Radio button</th>
<th>Checkbox</th>
</tr>
<tr>
<td>It is used when only one option to be selected out of several available options.</td>
<td>Checkbox allows one or many options to be selected.</td>
</tr>
<tr>
<td>It is created by using HTML <input> tag but type attribute is set to radio.</td>
<td>It is also created using HTML <input> tag but type attribute is set to checkbox.</td>
</tr>
<tr>
<td>It is a single control unit.</td>
<td>It is a multiple control unit.</td>
</tr>
<tr>
<td>Radio button is presented as a small circle on the screen.</td>
<td>Checkbox is presented as a small square box on the screen.</td>
</tr>
<tr>
<td>Radio button have only 2 states namely- True & False.</td>
<td>Checkbox have 3 states namely- Checked, unchecked & indeterminate.</td>
</tr>
<tr>
<td>It is used when you want to limit the users choice to just one option from the range provided.</td>
<td>It is used when you want to allow user to select multiple choices.</td>
</tr>
</table>
<div class="_ap_apex_ad" id="82d2079a-8120-480f-9fc7-5cda825d56e7" style="min-height:280px"></div>
<p></p></div>
</article>

                </div>

            </div>
        </div>
    </div>
</div>


  
<footer>
    <div class="bg-white text-center pb-1">
        <p class="text-body-tertiary pt-3 lh-lg text-opacity-50" id="footer-text">Copyright © 2020 - 2024 版权所有
            <br>

            <a href="https://beian.miit.gov.cn/" target="_blank"
                class="text-opacity-50 text-body-tertiary mt-1 mb-1">蜀ICP备20006366号-1</a>

            <br>
            Made with ❤️ in Chengdu
        </p>
    </div>
</footer>


  <!-- 引入Bootstrap JavaScript库 -->
  <script src="https://unpkg.com/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"
    crossorigin="anonymous"></script>

  <!-- 引入Meilisearch搜索相关的JavaScript库 -->
  <script
    src="https://cdn.jsdelivr.net/npm/@meilisearch/instant-meilisearch/dist/instant-meilisearch.umd.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/instantsearch.js@4"></script>
  <script src="https://imangodoc.com/static/javascript/meili_custom.js"></script>

  <!-- 引入prismjs代码高亮相关的JavaScript库 -->
  <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-core.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/toolbar/prism-toolbar.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js"></script>
  <script
    src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js"></script>

</body>


</html>