Html 默认情况下如何选择单选按钮?

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

How to select a radio button by default?

htmlradio-button

提问by jslearner

I have some radio buttons and I want one of them to be set as selected by default when the page is loaded. How can I do that?

我有一些单选按钮,我希望在加载页面时将其中一个设置为默认选中。我怎样才能做到这一点?

<input type="radio" name="imgsel"  value=""  /> 

回答by Shakti Singh

XHTML solution:

XHTML 解决方案:

<input type="radio" name="imgsel" value="" checked="checked" />

Please note, that the actual value of checkedattribute does not actually matter; it's just a convention to assign "checked". Most importantly, strings like "true"or "false"don't have any special meaning.

请注意,checked属性的实际值实际上并不重要;这只是分配的约定"checked"。最重要的是,字符串喜欢"true""false"没有任何特殊含义。

If you don't aim for XHTML conformance, you can simplify the code to:

如果您的目标不是 XHTML 一致性,您可以将代码简化为:

<input type="radio" name="imgsel" value="" checked>

回答by mdm

Use the checked attribute.

使用checked 属性。

<input type="radio" name="imgsel"  value="" checked /> 

or

或者

<input type="radio" name="imgsel"  value="" checked="checked" /> 

回答by discodane

This doesn't exactly answer the question but for anyone using AngularJS trying to achieve this, the answer is slightly different. And actually the normal answer won't work (at least it didn't for me).

这并不能完全回答这个问题,但对于任何使用 AngularJS 试图实现这一目标的人来说,答案略有不同。事实上,正常的答案是行不通的(至少对我来说不是)。

Your html will look pretty similar to the normal radio button:

您的 html 看起来与普通单选按钮非常相似:

<input type='radio' name='group' ng-model='mValue' value='first' />First
<input type='radio' name='group' ng-model='mValue' value='second' /> Second

In your controller you'll have declared the mValuethat is associated with the radio buttons. To have one of these radio buttons preselected, assign the $scopevariable associated with the group to the desired input's value:

在您的控制器中,您将声明mValue与单选按钮关联的 。要预先选择这些单选按钮之一,请将$scope与组关联的变量分配给所需的输入值:

$scope.mValue="second"

This makes the "second" radio button selected on loading the page.

这使得在加载页面时选择“第二个”单选按钮。

EDIT: Since AngularJS 2.x

编辑:从 AngularJS 2.x

The above approach does not work if you're using version 2.x and above. Instead use ng-checkedattribute as follows:

如果您使用的是 2.x 及更高版本,则上述方法不起作用。而是使用ng-checked属性如下:

<input type='radio' name='gender' ng-model='genderValue' value='male' ng-checked='genderValue === male'/>Male
<input type='radio' name='gender' ng-model='genderValue' value='female' ng-checked='genderValue === female'/> Female

回答by Gerben Jacobs

Add this attribute:

添加此属性:

checked="checked"

回答by Rohjay

They pretty much got it there... just like a checkbox, all you have to do is add the attribute checked="checked" like so:

他们几乎得到了它......就像一个复选框,你所要做的就是添加属性checked="checked",如下所示:

<input type="radio" checked="checked">

...and you got it.

......你明白了。

Cheers!

干杯!