javascript - AngularJS 当一个单选按钮值被选择为 true 时,如何将所有单选按钮值更新为 false?

标签 javascript html angularjs radio-button

我正在从数据库中提取单独的真/假字符串值。 我给出的示例只是为了说明我实际上从数据库中获取的内容,即 true 或 false 的字符串值,我无法更改它。

我想做的是让一个单选按钮将其值显示为选中(如果为 true),如果为 false,则不选择。

这一点在我下面的答案中工作得很好,但是当用户选择不同的按钮时,我想将该值设置为“true”并将所有其他值设置为“false”。

我尝试了各种方法,包括 ng-checked,但这不适用于 ng-model。我发现 AngularJS 中的单选按钮非常令人困惑,请问有人可以帮助我理解我应该做什么吗?谢谢你

    <!doctype html>
<html ng-app="sample" >
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Test</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link rel="stylesheet" href="app/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="app/css/style.css">
    <script src="node_modules/jquery/dist/jquery.min.js"></script>
    <script src="app/bootstrap/js/bootstrap.min.js"></script>
    <script src="node_modules/angular/angular.js"></script>
    <script>
        var MyApp = angular.module('sample', []);

        MyApp.controller('MainCtrl', function($scope) {

            $scope.red = 'true';
            $scope.yellow = 'false';
            $scope.blue = 'false';
            $scope.green = 'false';
            $scope.orange = 'false';
            $scope.purple = 'false';
            $scope.black = 'false';
            $scope.pink = 'false';
            $scope.white = 'false';

        });     
    </script>  
</head>

<body ng-controller="MainCtrl">
    <div class="container" ><!-- ng-repeat="x in alien.colours" -->

        <h3>What colour alien are you?</h3>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="red" />
        <label>Red</label>&nbsp; Value is {{red}}<br>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="yellow" />
        <label>Yellow</label>&nbsp; Value is {{yellow}}<br>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="blue" />
        <label>Blue</label>&nbsp; Value is {{blue}}<br>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="green" />
        <label>Green</label>&nbsp; Value is {{green}}<br>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="orange" />
        <label>Orange</label>&nbsp; Value is {{orange}}<br>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="purple" />
        <label>Purple</label>&nbsp; Value is {{purple}}<br>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="black" />
        <label>Black</label>&nbsp; Value is {{black}}<br>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="pink" />
        <label>Pink</label>&nbsp; Value is {{pink}}<br>

        <input type="radio" name="AliCol" ng-value="'true'" ng-model="white" />
        <label>White</label>&nbsp; Value is {{white}}<br>
    </div>
</body>

</html>

下面是当您选择不同按钮时发生的情况,值更新为“true”按钮但不能设置为“false”

example

最佳答案

做到这一点的一种方法(一种非常基本的方法,可以改进很多!)是使用一组对象来为您完成这项工作。

fiddle :https://jsfiddle.net/t3t6kueL/

在你的 Javascript 中,你可以这样做:

  var MyApp = angular.module('sample', []);

  MyApp.controller('MainCtrl', function($scope) {

    $scope.colors = [{
        name: 'Red', // name => For presentation
        value: 'false' // value => For toggling the group's values
      },
      {
        name: 'Yellow',
        value: 'false'
      },
      {
        name: 'Blue',
        value: 'false'
      },
      {
        name: 'Green',
        value: 'false'
      },
      {
        name: 'Orange',
        value: 'false'
      },
      {
        name: 'Purple',
        value: 'false'
      },
      {
        name: 'Black',
        value: 'false'
      },
      {
        name: 'Pink',
        value: 'false'
      },
      {
        name: 'White',
        value: 'false'
      },
    ]

    // Expose a method to change the group's values
    $scope.selectColor = function(selectedColorName) {
      // Loop through all of the colors
      $scope.colors.forEach(function(color) {
        // If this is what's being set, then set this to true
        // Note: If there are two different objects, with the same name, you will end up with 2 true values here... 
        if (color.name === selectedColorName) {
          color.value = 'true';
        // For all the others, set it to false
        } else {
          color.value = 'false';
        }
      });
    }
  });

这将允许您移动逻辑并处理要呈现给应用程序代码的内容,使您的 HTML 更加简洁,如下所示:

<div class="container">

  <h3>What colour alien are you?</h3>

  <div ng-repeat="color in colors">
    <input type="radio" name="AliCol" ng-click="selectColor(color.name)" />
    <label>{{color.name}}</label>&nbsp; Value is {{color.value}}<br>
  </div>

</div>

关于javascript - AngularJS 当一个单选按钮值被选择为 true 时,如何将所有单选按钮值更新为 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50670143/

相关文章:

javascript - Facebook "zoom in"时,聊天侧边栏被 Conceal ,但如何?

javascript - ContentEditable div 在 JS fiddle 中无法正常工作

javascript - 禁用位置 :fixed on scrolling when 250px from bottom of the page

json - angularjs向json数据添加一列

javascript - 如何使用下拉按钮显示范围?

javascript - 如何在 AngularJS Controller 中执行 if 语句

php - 如何在链接单击上插入和删除 div 及其内容

javascript - Angular 跨度条件悬停在文本上

php - 发送 header 后关闭网页(php)

javascript - 如何将 SVG 嵌入到来自 Cross Origin url 的 html <object> 标签中(s3_url/image.svg)