【问题标题】:How to correctly use radio button to get selected value如何正确使用单选按钮获取选定值
【发布时间】:2016-11-29 17:58:35
【问题描述】:

我需要获取所选单选按钮的价格(值)并在 JS 上使用它然后返回它并在 HTML 上显示

Javascript:

      $scope.bases = [{
      name:'Tomato',
      price:3
    },{
      name:'Cream',
      price:4
    }];

HTML:

<label for="bases">Base :</label>
      <ul name="bases">
        <li ng-repeat="base in bases">
          <input type="radio" name="bases" ng-value="{{base.prix}}" ng-model="selectedValue">{{base.nom}} {{base.prix}} €</li>
      </ul>
...
{{price}}

函数JS:

$scope.PriceBase = function()
{
      var price = 0;
      // Dunno => get price of radio button
      // Calculate on it
      price + 1.20;
      // Return it to show it on HTML
      return price;

};

【问题讨论】:

  • 什么是“base.prix”和“base.nom”?从哪里可以找到它的价值?

标签: javascript html angularjs


【解决方案1】:

var app = angular.module('myApp', []);
app.controller('myController', function($scope) {

  $scope.selected= {
    name: "Cream",
    price: 5.2
  };
  $scope.items= [{
    name: "Tomato",
    price: 3
  }, {
    name: "Cream",
    price: 4
  }];
  $scope.PriceChangeRateBase = function(priceValue) {
    $scope.selected.price= parseInt(priceValue) + 1.20;;

  };
  $scope.getItems = function() {
    return $scope.items;
  };

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="myController">
    <div>Which one?</div>
    <label class="radio" ng-repeat="item in getItems()">
      <input type="radio" ng-model="selected.name" name="itemOptions" ng-change="PriceChangeRateBase(item.price)" value="{{item.name}}">{{item.name}} - ${{item.price}}
    </label>
    <hr />
    <div>You picked: {{selected.name}} - ${{selected.price}}</div>
  </div>
</div>

【讨论】:

    【解决方案2】:

    试试这个代码而不是你的代码

    <label for="bases">Base :</label>
          <ul name="bases">
            <li ng-repeat="base in bases">
              <input type="radio" name="bases" ng-change="PriceBase(base.price)" ng-value="{{base.price}}" ng-model="selectedValue">{{base.name}} {{base.price}} €</li>
          </ul>
    ...
    {{price}}
    

    控制器

    $scope.PriceBase = function(valueOfrdb)
    {
          $scope.price = valueOfrdb;
          // Dunno => get price of radio button
          // Calculate on it
         return parseInt(price) + 1.20;          
    };
    

    编辑

    //或者这样试试

    <label for="bases">Base :</label>
              <ul name="bases">
                <li ng-repeat="base in bases">
                  <input type="radio" name="bases" ng-change="PriceBase()" ng-value="{{base.price}}" ng-model="selectedValue">{{base.name}} {{base.price}} €</li>
              </ul>
        ...
        {{price}}
    

    //控制器

        $scope.PriceBase = function(valueOfrdb)
        {
          if($scope.selectedValue!=undefined)
          {
           return parseInt($scope.selectedValue) + 1.20;          
          }
          return 0.00
        };
    

    【讨论】:

    • 我认为你应该先检查一下单选按钮是否被选中
    • @Kgn-web ng-change="PriceBase(base.price)" 这个 ng-change 属性只触发点击单选按钮
    • 是的。你是对的。但我要求你设置一个 if 条件来检查单选按钮是否被选中
    • if(selectedValue){ }
    • 我已经通过参数传递了PriceBase(base.price)" 方法的单选按钮值。所以不需要这样做。 ng-model 表示数据概念的两种方式绑定。在这种情况下不需要
    【解决方案3】:
    $scope.PriceBase = function()
    {
    var price = 0;
    var radios = document.getElementsByName('bases');
    
    for (var i = 0, length = radios.length; i < length; i++) {
        if (radios[i].checked) {
    
            price=radios[i].value;
    
            // only one radio can be logically checked, don't check the rest
            break;
        }
    }
     price + 1.20;
          // Return it to show it on HTML
          return price;
    });
    

    【讨论】:

    • 你为什么在这里使用循环?如果 OP 有 1000 条记录,那么无论何时单击单选按钮,您的代码都将花费大量时间来加载。
    • 循环用于单选按钮而不是记录。它将检查名称为 basses 的单选按钮,并仅选择选中的单选按钮的值。当它找到一个选中的单选按钮时会发生中断
    猜你喜欢
    • 1970-01-01
    • 2011-04-21
    • 2013-03-28
    • 1970-01-01
    • 2013-06-02
    • 2011-05-07
    • 2011-07-28
    • 2016-08-30
    相关资源
    最近更新 更多