Html 单选按钮检查不检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8711559/
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 12:18:41  来源:igfitidea点击:

Radio Button Checked not checking

htmlxhtmlforms

提问by Satch3000

I am having a problem with the following code:

我在使用以下代码时遇到问题:

<div>
<span>Item 1</span>
<input type="radio" name="radio1" id="radio1" checked="checked" />
</div>

<div>
<span>Item 2</span>
<input type="radio" name="radio1" class="checkbox" id="radio2" />
</div>

I have added checked="checked" to the first checkbox but the one checked is the second one for some reason.

我已将 check="checked" 添加到第一个复选框,但由于某种原因,选中的复选框是第二个复选框。

采纳答案by Phil

Judging by the code you posted, something in the missing code must be wronge. I put your code in a jsfiddle (1) and it works like a charm.

从您发布的代码来看,丢失的代码中的某些内容一定是错误的。我把你的代码放在一个 jsfiddle ( 1) 中,它就像一个魅力。

According to w3schools, checked="checked"is correct (http://www.w3schools.com/TAGS/att_input_checked.asp). I often use <input type="radio" "name="foo" checked />, which also works.

根据 w3schools,checked="checked"是正确的 (http://www.w3schools.com/TAGS/att_input_checked.asp)。我经常使用<input type="radio" "name="foo" checked />,这也有效。

More infos about the site would be helpful.

有关该网站的更多信息会有所帮助。

回答by Priyanka Kharote

When there are multiple radio buttons with the same idand/or nameattributes, only last value is used.

当有多个具有相同id和/或name属性的单选按钮时,仅使用最后一个值。

Change the nameattribute for each radio button and it will work.

更改name每个单选按钮的属性,它将起作用。

回答by Pankaj Upadhyay

it's because you are using the same name....id and names should be unique. Replace the name in second checkbox from radio1 to radio2

这是因为您使用相同的名称....id 和名称应该是唯一的。将第二个复选框中的名称从 radio1 替换为 radio2