如何知道是否选中了复选框?

时间:2017-04-19 03:56:16

标签: javascript angularjs ionic-framework

我正在寻找过去2小时内解决此问题的方法,但我无法找到解决问题的简单答案。

我想检查是否选中了复选框,无论我做什么,控制台总是告诉我它未经检查。

HTML:

<ion-checkbox ng-model="option1" ng-true-value="2" ng-false-value="0" id="itemOrder-checkbox1">With option 1 (+$2)</ion-checkbox>
      <ion-checkbox ng-model="option2" ng-true-value="3" ng-false-value="0" id="itemOrder-checkbox2">With option 2 (+$3)</ion-checkbox>
      <ion-checkbox ng-model="option3" ng-true-value="1" ng-false-value="0" id="itemOrder-checkbox3">With option 3 (+$1)</ion-checkbox>
      <label class="item item-input" id="itemOrder-input4">
        <input id="notes" type="text" placeholder="Add some notes...">
      </label>
      <div class="spacer" style="width: 300px; height: 33px;"></div>
      <button class="button button-block button-dark" style="color:#F5E124;" type="submit" id="itemOrder-button3" ng-click="addToCart()">
          Add to cart (${{quantity * (15 + option1 + option2 + option3)}})
      </button>

控制器:

$scope.addToCart = function(){
    if ($scope.option1) {
        console.log("CheckBox is checked.");
    } else {
        console.log("CheckBox is not checked.");
    }

    firebase.database().ref('accounts/' + userId + '/cart/').push({
      item: "item1",
      quantity: document.getElementById('quantity').value,
      price: document.getElementById('price').textContent,
      notes: document.getElementById('notes').value,

    })
  $state.go("menu2.menu");
  }

3 个答案:

答案 0 :(得分:1)

<html>
<head>
    <title></title>
</head>
<body>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
    <script type="text/javascript">
        var app = angular.module('MyApp', [])
        app.controller('MyController', function ($scope, $window) {
            $scope.CheckPassport = function () {
                if ($scope.HasPassport) {
                    $window.alert("CheckBox is checked.");
                } else {
                    $window.alert("CheckBox is not checked.");
                }
            };
        });
    </script>
    <div ng-app="MyApp" ng-controller="MyController">
        Do you have Passport?
        <label for="chkPassport">
            <input id="chkPassport" type="checkbox" ng-model="HasPassport" />
        </label>
        <input type="button" value = "Check Passport" ng-click = "CheckPassport()" />
    </div>
</body>
</html>

答案 1 :(得分:0)

<input type="checkbox" checked ng-change="click()" ng-model="option1" value="Bike">
    I have a bike

<强>控制器:

$scope.click = function(){
    if ($scope.option1) {
        alert("CheckBox is checked.");
    } else {
        alert("CheckBox is not checked.");
    }
  }

以下是演示:Plnkr

答案 2 :(得分:0)

使用{"data":{"data_0":{"name":"Name 0"},"data_1":{"name":"Name 1"}},"data_2":{"name":"Name 2"}} 指令复选框。

试试这个

&#13;
&#13;
ng-checked
&#13;
&#13;
&#13;