【问题标题】:Radio button is not working properly单选按钮无法正常工作
【发布时间】:2013-11-11 17:05:08
【问题描述】:

在我的网页中,我放置了一些单选按钮。但这些按钮无法正常工作。我可以检查多个按钮。

代码:

<label for="abc" style="margin-top:-20px;margin-left:40px">xyz</label>
<input type="radio" id="abc" name="abc" >        
<label for="bcd" style="margin-top:-20px;margin-left:40px">abc</label>
<input type="radio" id="bcd" name="bcd" >
<label for="efg" style="margin-top:-20px;margin-left:40px">ccc</label>
<input type="radio" id="efg" name="efg" >

fiddle

我只想检查一个按钮。请任何人帮助我。

【问题讨论】:

    标签: html radio-button


    【解决方案1】:

    因为name 属性的值不同,所以它们应该有一个共同的name 值,就像您对项目进行分组一样......例如

    <input type="radio" name="group1" />
    <input type="radio" name="group1" />
    <input type="radio" name="group1" />
    
    <!-- You can select any one from each group -->
    
    <input type="radio" name="group2" />
    <input type="radio" name="group2" />
    <input type="radio" name="group2" />
    

    Demo

    【讨论】:

    • 啊,伙计,这让我有些沮丧。有时回到基本面是件好事!谢谢。
    【解决方案2】:
    <label for="abc" style="margin-top:-20px;margin-left:40px">xyz</label>
    <input type="radio" id="abc" name="abc" >        
    <label for="bcd" style="margin-top:-20px;margin-left:40px">abc</label>
    <input type="radio" id="bcd" name="abc" >
    <label for="efg" style="margin-top:-20px;margin-left:40px">ccc</label>
    <input type="radio" id="efg" name="abc" >
    

    所有输入必须具有相同的 name="" 属性值

    【讨论】:

      【解决方案3】:

      组合在一起的单选按钮必须具有相同的区分大小写的name 属性。

      <label for="input1">First Input</label>
      <input type="radio" id="input1" name="inputGroup" >
      <label for="input2">Second Input</label>
      <input type="radio" id="input2" name="inputGroup" >
      <label for="input3">Third Input</label>
      <input type="radio" id="input3" name="inputGroup" >
      

      JSFiddle demo.

      来自HTML specification:

      单选按钮类似于复选框,只是当多个按钮共享同一个控件name时,它们是互斥的。

      【讨论】:

        【解决方案4】:

        名称属性需要相同。名称将单选按钮组合在一起,使它们成为一个单元。

        【讨论】:

          【解决方案5】:

          以相同的方式命名它们,在您的 php 或接收代码中,它将类似于

          $_POST['name'] = 'value of selected radio button'
          

          【讨论】:

            【解决方案6】:

            名称设置告诉该字段属于哪一组单选按钮。当您选择一个按钮时,同一组中的所有其他按钮都将被取消选择。 如果您无法定义当前按钮属于哪个组,则每个页面上只能有一组单选按钮。 例如:

            <input type="radio" name="fruit1" value="Apple"> Apple <br>
            <input type="radio" name="fruit1" value="Apricot" checked> Apricot <br>
            <input type="radio" name="fruit1" value="Avocado"> Avocado
            <hr>
            <input type="radio" name="fruit2" value="Banana"> Banana<br>
            <input type="radio" name="fruit2" value="Breadfruit"> Breadfruit<br>
            <input type="radio" name="fruit2" value="Bilberry" checked>  Bilberry
            

            【讨论】:

              【解决方案7】:

              为要从中选择一个选项的所有单选按钮指定相同的名称

              <label for="abc" style="margin-top:-20px;margin-left:40px">xyz</label>
              <input type="radio" id="abc" name="abc" >        
              <label for="bcd" style="margin-top:-20px;margin-left:40px">abc</label>
              <input type="radio" id="bcd" name="abc" >
              <label for="efg" style="margin-top:-20px;margin-left:40px">ccc</label>
              <input type="radio" id="efg" name="abc" >
              

              现在它可以正常工作了

              【讨论】:

                【解决方案8】:

                赋予名称相同的属性。 选中的属性可用于指示应选择哪个值。在您要检查的值的语法中某处写checked="checked"

                【讨论】:

                  【解决方案9】:

                  我在搜索关键字“html radio not working”时找到了这个线程。查看我的代码后,我注意到我使用了一个 javascript 方法“event.preventDefault();”在我创建的这个自定义函数中,在该自定义函数中触发此事件的 HTML 节点是“不工作”的无线电父节点。所以我解决了删除“event.preventDefault();”的问题。如果将来有人到达此线程,我希望这能以某种方式帮助您(即使出于调试目的)。

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 2013-08-26
                    • 2013-03-27
                    • 2012-12-28
                    • 1970-01-01
                    • 2021-11-21
                    相关资源
                    最近更新 更多