Html 单选按钮无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19710804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:56:04  来源:igfitidea点击:

Radio button is not working properly

htmlradio-button

提问by James

In my web page I have placed some radio buttons. But these buttons are not working properly. I can check multiple buttons.

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

code:

代码:

<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

小提琴

I want to check only one button. Please any one help me.

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

回答by Mr. Alien

Because you've different value for nameattribute, they should have a common namevalue, just like you group items.. for example

因为你有不同的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

演示

回答by Different vision

<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" >

All inputs must have the same name="" attribute value

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

回答by James Donnelly

Radio buttons which are grouped together must have the same case-sensitive nameproperty.

组合在一起的单选按钮必须具有相同的区分大小写的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.

JSFiddle 演示

From the HTML specification:

HTML 规范

Radio buttons are like checkboxes except that when several share the same control name, they are mutually exclusive.

单选按钮就像复选框,除了当几个共享相同的控件时name,它们是互斥的。

回答by 19greg96

Name them the same way, and in your php or receiving code it will be something like

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

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

回答by RealityDysfunction

Name attribute needs to be the same. Name groups radio buttons together to make them one unit.

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

回答by Saurabh Chandra Patel

The name setting tells which group of radio buttons the field belongs to. When you select one button, all other buttons in the same group are unselected. If you couldn't define which group the current button belongs to, you could only have one group of radio buttons on each page. e.g :

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

<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

回答by Junaid Nazir

Give same name to all radio button from which you want to select one option

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

<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" >

Now it will work properly

现在它将正常工作

回答by Shulkin

Give the name the same attribute. The checked attribute can be used to indicate which value should be selected. Somewhere in your syntax for the value you want checked write checked="checked"

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