javascript - 如何切换单选按钮

标签 javascript html angularjs

我只想在单选按钮之间切换。该值以 null 开头。

如果为 0,则选中第一个复选框,取消选中第二个复选框

如果为 1,则选中第二个复选框,取消选中第一个复选框

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="">
  <span>Value: {{vm.value}}</span> <br>

  <input type="radio" ng-model="vm.value" value={{vm.value}}
         ng-click="vm.value = 0" ng-checked="vm.value == 0"
         ng-init="vm.value = null" />

  <input type="radio" ng-model="vm.value" ng-click="vm.value = 1"
         value={{vm.value}} ng-checked="vm.value == 1" />
</div>

有什么帮助吗?

最佳答案

Theres no need to use ng-click and ng-checked.

  • 您必须将每个 radio 的名称设置为相同,例如下面示例中的 name="myRadios"

  • 您必须将设置为01而不是{{vm.value}} 或将其绑定(bind)到您想要的数据。

    (您的 ng-model="vm.value"value="{{vm.value}}" 没有任何意义,因为 ng-model= 是您要存储值的部分,value= 是值的来源部分。)

<小时/>

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="">
  <span>Value: {{vm.value}}</span> <br>

  <input type="radio" name="myRadios" ng-model="vm.value" value="0" ng-init="vm.value = null" />

  <input type="radio" name="myRadios" ng-model="vm.value" value="1" />
</div>

关于javascript - 如何切换单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58765500/

相关文章:

javascript - 在 Angular Controller 中监听文档事件

angularjs - 来自 Angular 的错误,AngularJS 1.3 想要 ngMessages 注入(inject)

javascript - KeyboardAvoidingView "Padding"无法正常工作

javascript - 如何从 click() 函数内的动画队列返回 true/false?

javascript - 页面上的随机元素

html - 为什么这些 float 的 div 不能正确显示?

javascript - 在asp文件中使用html按钮元素javascript将导致页面重新加载

javascript - 条件满足 ngShow AND ngHide

javascript - jQuery Mobile 对话框立即关闭

php - 在 jQuery 中设置?