如何在AngularJS中获取所选单选按钮的值 [英] How to get value of selected radio button in angularjs

查看:436
本文介绍了如何在AngularJS中获取所选单选按钮的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个单选按钮passfail.

如何获取所选单选按钮的值.

How to get value of selected radio button.

<div class="col-md-4">
            Result
            <div class="radio">
                <label><input type="radio" name="rdoResult">pass</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="rdoResult">fail</label>
            </div>
        </div>

我是否需要使用ng-model或其他东西.在jquery中,我知道Angularjs中没有的东西.

Do I need to use ng-model or something else. In jquery I know the things well not in angularjs.

推荐答案

两者都应具有相同的ng-model,并且具有不同的 ng-value (旨在与选择option s或radio按钮一起使用),以便在result $ scope上更改所选值变量,则可以在表单提交或单击按钮时在控制器内获取该值.

Both should have same ng-model with different ng-value(meant for use with select options or radio button), so that the selected value will be changed on result $scope variable and you can grab that value inside a controller on form submit or button click.

标记

<div class="col-md-4">
    Result
    <div class="radio">
        <label>
            <input ng-model="result" type="radio" name="rdoResult" ng-value="'pass'">
              pass
        </label>
    </div>
    <div class="radio">
        <label>
            <input ng-model="result" type="radio" name="rdoResult" ng-value="'fail'">
              fail
        </label>
    </div>
</div>

这篇关于如何在AngularJS中获取所选单选按钮的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆