AngularJs输入无线电值

时间:2016-03-28 11:10:17

标签: javascript angularjs

我有一个表单,用户必须说明他是male还是female。在我的数据库中,它是10。我怎么能用AngularJs input radio做到这一点。

现在我有了这个:

<div class="form-group">
    <label class="col-md-2 control-label">Radios</label>

    <div class="col-md-10">
        <div class="radio radio-primary">
            <label>
                <input type="radio" name="male" id="male" ng-model="employeeCtrl.employee.Gender" ng-value="0"><span class="circle"></span><span class="check"></span>
                Male
            </label>
        </div>
        <div class="radio radio-primary">
            <label>
                <input type="radio" name="optionsRadios" id="optionsRadios4" ng-model="employeeCtrl.employee.Gender" ng-value="1"><span class="circle"></span><span class="check"></span>
                Female
            </label>
        </div>
    </div>
</div>

但这不起作用。未选择右侧单选按钮,值也不会更改。

1 个答案:

答案 0 :(得分:1)

运行代码,看看这是你所期望的。 对单选按钮使用相同的名称,您可以为每个控制器使用不同的ID。

&#13;
&#13;
<!DOCTYPE html>
<html>

  <head>
    <script data-require="angular.js@1.5.0" data-semver="1.5.0" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app="">
    
    <div class="form-group">
    <label class="col-md-2 control-label">Radios</label>

      <div class="col-md-10">
          <div class="radio radio-primary">
              <label>
                  <input type="radio" id='gender_male' name="gender" ng-model="employeeCtrl.employee.Gender" ng-value="0"><span class="circle"></span><span class="check"></span>
                  Male
              </label>
          </div>
          <div class="radio radio-primary">
              <label>
                  <input type="radio" id='gender_female' name="gender" ng-model="employeeCtrl.employee.Gender" ng-value="1"><span class="circle"></span><span class="check"></span>
                  Female
              </label>
          </div>
      </div>
    Gender: {{employeeCtrl.employee.Gender}}
    </div>

  </body>

</html>
&#13;
&#13;
&#13;