我已在选择框中正确添加更改事件,但它无法正常工作。任何领导都将不胜感激。谢谢提前
HTML
[<select id="notifyBy" ng-change="selectchange()" style="border:none" class="formtext1" ng-model="selectvalue" >
<option value="1">E-mail, telephone, etc.</option>
<option>Telephone No:</option>
<option>Telephone No:</option>
<option>Telephone No:</option>
<option>Telephone No:</option>
</select>][1]
controller
angular.module('starter.controllers', [])
[.controller('AppCtrl',\['$scope',function($scope, $ionicModal, $timeout,$location,$ionicPopover,$http,$ionicNativeTransitions) {
$scope.selectchange = function() {
alert("enterd");
}][1]
}])
答案 0 :(得分:4)
您缺少选项的价值
<option value="2">Telephone No:</option>
<强>详情
<option value="1">E-mail, telephone, etc.</option>
<option >Telephone No:</option> <!--Added value attribute here -->
<option >Telephone No:</option> <!--Added value attribute here -->
这里的问题是
当您从option1(电子邮件,电话等)将值更改为选项2(电话号码:)时,会触发onchange事件
当您从选项2(电话号码:)将值更改为选项3(电话号码:)时,它不会触发onchange事件,因为值没有变化
<强>解决方案强>
所以你应该为所有人提供不同的价值。
解决方案1:
<option value="2">Telephone No:</option> <!--Added value attribute here -->
<option value="3">Telephone No:</option> <!--Added value attribute here -->
解决方案2:
<option>Telephone No:1</option> <!--Added value attribute here -->
<option>Telephone No:2</option> <!--Added value attribute here -->
angular.module('starter', [])
.controller('AppCtrl', ['$scope',
function($scope, $ionicModal, $timeout, $location, $ionicPopover, $http, $ionicNativeTransitions) {
$scope.selectchange = function() {
alert("enterd");
}
}
])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="starter" ng-controller="AppCtrl">
<select id="notifyBy" ng-change="selectchange()" style="border:none" class="formtext1" ng-model="selectvalue">
<option value="1">E-mail, telephone, etc.</option>
<option value="2">Telephone No:</option> <!--Added value attribute here -->
<option value="3">Telephone No:</option> <!--Added value attribute here -->
<option value="4">Telephone No:</option>
<option value="5">Telephone No:</option>
</select>
</div>
答案 1 :(得分:0)
问题是因为您一次又一次地选择相同的值。
尝试为每个选项添加值。
<强>样本强>
var app = angular.module('todoApp', []);
app.controller("dobController", ["$scope",
function($scope) {
$scope.selectchange = function() {
alert("enterd");
}
}
]);
&#13;
<!DOCTYPE html>
<html ng-app="todoApp">
<head>
<title>To Do List</title>
<link href="skeleton.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
</head>
<body ng-controller="dobController">
<select id="notifyBy" ng-change="selectchange()" style="border:none" class="formtext1" ng-model="selectvalue">
<option value="1">E-mail, telephone, etc.</option>
<option value="2">Telephone No:</option>
<option value="3">Telephone No:</option>
<option value="4">Telephone No:</option>
<option value="5">Telephone No:</option>
</select>
</body>
</html>
&#13;
答案 2 :(得分:0)
Angular似乎很难,因此很少有时间我们会从头到尾犯错误,这里有一些我认为不完整的事情。
非常好在这里:
angular.module('starter.controllers', [])
.controller('AppCtrl',['$scope',function($scope) {
$scope.selectchange = function() {
alert("enterd");
}
}]);
angular.bootstrap(document, ['starter.controllers']);
JSFiddle链接:https://jsfiddle.net/Ashokkumargupta/8a7zxdq4/1/
干杯, 阿肖克