我在用户模型中有选择字段:
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default='M')
在我的模板中我想使用单选按钮:
<div class="radio-item">
<div class="label-text">Пол</div>
<div class="item">
<input type="radio" name="sex" id="male">
<label for="male"><span></span> Male</label>
</div>
<div class="item">
<input type="radio" name="sex" id="female">
<label for="female"><span></span> Female</label>
</div>
</div>
答案 0 :(得分:1)
您似乎从API获取数据并绑定到表单。因此,在您的情况下,您可以ng-model
使用radio
输入。
但在第一件事之前,你需要将整个对象绑定到$scope.user = data;
。另外我假设无线电值是M
&amp; F
。
<强> HTML 强>
<div class="radio-item">
<div class="label-text">Пол</div>
<div class="item">
<input type="radio" name="sex" id="male" ng-model="user.gender" value="M">
<label for="male"><span></span> Male</label>
</div>
<div class="item">
<input type="radio" name="sex" id="female" ng-model="user.gender" value="F">
<label for="female"><span></span> Female</label>
</div>
</div>