简体   繁体   English

如何为默认单选按钮设置一个值?

[英]How to set a default radio button with a value?

General issue: I cannot set a default radio button in my radio groups unless I remove the value attribute from the inputs. 常见问题:除非从输入中删除value属性,否则无法在单选组中设置默认单选按钮。

Specifics: Each input has a value that is needed as it is being used by angular in other places in the app. 具体说明:每个输入都有一个所需的值,因为在应用程序的其他位置,Angular正在使用它。 So, I need to know how to set an input to default checked while retaining the values on the inputs. 因此,我需要知道如何将输入设置为默认选中,同时保留输入上的值。 It has the same behavior if I try to add checked with jQuery instead of in HTML. 如果我尝试使用jQuery而不是HTML添加检查,它具有相同的行为。

I have looked at several related questions including here . 我已经看了几个相关的问题,包括这里 The specific issue is I need to have values and be able to set one input to checked as a default. 具体问题是我需要具有值,并且能够将一个输入设置为默认值。 The first input is set to checked, but it does not actually work unless I remove the value from that input. 第一个输入设置为选中,但是除非我从该输入中删除该值,否则它实际上不起作用。

Here is the HTML: 这是HTML:

<div class="radio">
   <label>
        <input type="radio" ng-model="optionsRadios" id="new" value="new" checked>
        New
   </label>

   <label>
        <input type="radio" ng-model="optionsRadios" id="used" value="used">
        Used
   </label>

   <label >
         <input type="radio" ng-model="optionsRadios" id="broken" value="broken">
         Broken
   </label>
</div>

You have to use ng-checked="true" instead of checked 您必须使用ng-checked="true"而不是checked

<input type="radio" ng-model="optionsRadios" id="new" value="new" ng-checked="true">

 <!DOCTYPE html> <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> <body ng-app=""> <p>My cars:</p> <input type="radio" ng-model="all" value="Volvo" ng-checked="true">Volvo<br> <input type="radio" ng-model="all" value="Ford">Ford<br> <input type="radio" ng-model="all" value="Mercedes">Mercedes </body> </html> 

If you have a group of radio button and you want to set radio button checked based on model, then radio button which has same value and ng-model is checked automatically. 如果您有一组单选按钮,并且要基于模型设置选中单选按钮,则会自动检查具有相同值和ng-model的单选按钮。

<input type="radio"
ng-model="optionsRadio" value="1" >
<input type="radio"
ng-model="optionsRadio" value="2" >
<input type="radio"
ng-model="optionsRadio" value="3" >

If the value of optionsRadio is "2" then second radio button will be selected automatically. 如果optionsRadio的值为“ 2”,则将自动选择第二个单选按钮。

In Your Case: When ur initiating the controller all u have to do is to assign a value to your model to keep atleast one radio button selected. 在您的情况下:启动控制器时,您要做的就是为模型分配一个值,以使至少一个单选按钮保持选中状态。

Controller part: 控制器部分:

$scope.optionsRadio = "new";

Form part: 表单部分:

<input type="radio" ng-model="optionsRadio" value="new" >
<input type="radio" ng-model="optionsRadio" value="used" >
<input type="radio" ng-model="optionsRadio" value="broken" >

First radio button will be selected automatically. 第一个单选按钮将被自动选择。

Note: If value doesn't work u can use ng-value . 注意:如果value不起作用,则可以使用ng-value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM