勾选一个初始值给单选button

如何在HTML中检查最初的单选button的值?

您可以使用checked属性来做到这一点:

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

你可以使用:

 <input type="radio" checked /> 

仅使用未指定值的已检查属性与checked="checked"

我已经把这个答案放在一个类似的问题上,这个问题被标记为这个问题的重复。 答案帮助了相当数量的人,所以我想我也会在这里添加它,以防万一。

这并不完全回答这个问题,但对于任何使用AngularJS试图实现这个目标的人来说,答案都有所不同。 而实际上正常的答案是行不通的(至less它不适合我)。

你的html看起来很像普通的单选button:

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

在您的控制器中,您将声明与单选button关联的mValue 。 要预先select这些单选button之一,将与该组关联的$scopevariables分配给所需的input值:

 $scope.mValue="second" 

这使加载页面时select“第二个”单选button。

对于任何正在寻找Angular2(2.4.8)解决scheme的人来说,在search时这是一个普遍受欢迎的问题:

 <div *ngFor="let choice of choices"> <input type="radio" [checked]="choice == defaultChoice"> </div> 

这会将checked属性添加到给定条件的input中,但如果条件失败则不会添加任何内容。

要这样做:

 [attr.checked]="choice == defaultChoice" 

因为这会将checked="false"属性添加到每个其他input元素。

由于浏览器仅查找checked属性( )的存在,忽略其值,因此将检查组中最后一个input。

另一种设置在单选button上选中的默认值的方法,特别是如果你使用的是angularjs,那么将'ng-checked'标志设置为“true”,例如: <input id="d_man" value="M" ng-disabled="some Value" type="radio" ng-checked="true"> Man

checked =“checked”对我不起作用。

请注意,如果您有两个具有相同“名称”属性的单选button,并且它们具有“必需”属性,那么向它们添加“checked”属性将不会使它们被检查。

例如:这使得单选button保持未选中状态。

 <input type="radio" name="gender" value="male" required <?php echo "checked"; ?>/> <input type="radio" name="gender" value="female" required /> 

这将使“男性”单选button被选中。

 <input type="radio" name="gender" value="male" <?php echo "checked"; ?>/> <input type="radio" name="gender" value="female" /> 
 <input name="sections" id="option1" type="radio"/> window.onload = function () { var input = document.getElementById('section1'); input.select(); }