【问题标题】:Is there a way to define a default value to be selected in an HTML Form of Radio Buttons?有没有办法定义在单选按钮的 HTML 表单中选择的默认值?
【发布时间】:2012-02-16 13:49:22
【问题描述】:
<body>
  <form>
    <input type="radio" name="amount" value="10"/> $10&#8194 
    <input type="radio" name="amount" value="25"/> $25&#8194 
    <input type="radio" name="amount" value="50"/> $50&#8194
    <input type="radio" name="amount" value="100"/> $100&#8194
    <input type="radio" name="amount" value="250"/> $250&#8194
    <input type="radio" name="amount" value="other"/> Other
  </form>
</body>

要使 $50 选项成为默认选择,我将使用什么 HTML 代码?

Link to code bin demo

【问题讨论】:

    标签: html forms radio-button radiobuttonlist


    【解决方案1】:

    只需将其设置为选中:

    <body>
      <form>
        <input type="radio" name="amount" value="10"/> $10&#8194 
        <input type="radio" name="amount" value="25"/> $25&#8194 
        <input type="radio" name="amount" value="50" checked="checked" /> $50&#8194
        <input type="radio" name="amount" value="100"/> $100&#8194
        <input type="radio" name="amount" value="250"/> $250&#8194
        <input type="radio" name="amount" value="other"/> Other
      </form>
    </body>
    

    【讨论】:

      【解决方案2】:

      只需添加

      checked="checked"
      

      在您希望默认选中的按钮声明中。

      【讨论】:

        【解决方案3】:
        <input type="radio" name="amount" value="50" checked="checked"/>
        

        【讨论】:

        • 我在检查它是否有效之前投票赞成。似乎对我不起作用:-\ 我知道 selected 方法用于在下拉列表中预选项目...demo code Documentation w3schools
        • 是的 - 我回答了,然后试了一下,发现我在考虑选择,而不是收音机,所以编辑它以检查。
        【解决方案4】:

        不用加“checked”:

        <input type="radio" name="amount" value="50" checked />
        

        【讨论】:

          猜你喜欢
          • 2012-05-25
          • 2013-06-08
          • 2016-12-17
          • 2017-10-02
          • 1970-01-01
          • 1970-01-01
          • 2013-07-20
          • 2013-03-01
          • 1970-01-01
          相关资源
          最近更新 更多