我尝试从单选按钮中获取所选值 但是,当我选择老师时,它仍然是学生"
Html文件:
<!DOCTYPE html>
<html ng-app="phase2">
<head>
<title>Sign UP Page</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<link href="style.css" rel="stylesheet" />
<script data-semver="1.2.28" src="https://code.angularjs.org/1.2.28/angular.js" data-require="angular.js@1.2.x"></script>
<script src="https://code.angularjs.org/1.2.28/angular-route.js"></script>
<script src="RigesterationController.js"></script>
</head>
<body >
<center>
<p>Enter Your User Name : <input type="text" , name="UserName" id ="UName" required /> </p>
<p>Enter Your Email : <input type="text" , name="Email" id ="email" required /> </p>
<p>Enter Your Password : <input type="password" , name="pass" id ="Pass" required/> </p>
<p>Choose Gender : <br> Male<input type="radio" name="gender" value="m" id="Gender" checked/> Female<input type="radio" name="gender" value="f" id="Gender"/> </p>
<p>Choose User Type :<br> Student<input type="radio" name="UserType" value="s" id="Utype" checked/> Teacher<input type="radio" name="UserType" value="t" id="Utype"/> </p>
<div ng-app="phase2" >
<div ng-controller="SignUP">
<input type="button" name="signup" value="SignUP" ng-click="save()" />
</div>
</div>
</center>
</body>
</html>
&#13;
RigesterationController.js文件:
var app = angular.module("phase2" , [])
app.controller( "SignUP" ,function ($scope , $http , srvShareData , $location)
{
$scope.dataToShare = [];
$scope.save = function() {
var email= document.getElementById("email").value;
var UName=document.getElementById("UName").value;
var Pass=document.getElementById("Pass").value;
var gender=document.getElementById("Gender").value;
var UserType=document.getElementById("Utype").value;
alert(UserType)
$http.get('http://localhost:8090/SignUp/'+email+'/'+UName+'/'+Pass+'/'+gender+'/'+UserType)
.then(function(response)
{
});
if (UserType=="t")
{
window.location.href="http://localhost:8060/TheAngular_Project/TeacherPage.html";
}
else if (UserType=="s")
{
window.location.href="http://localhost:8060/TheAngular_Project/StudentPage.html";
}
}
});
&#13;
尽管我选择老师,但我总能得到...如何获得选定的价值? 提前谢谢
答案 0 :(得分:1)
一些建议:
为什么ng-app="phase2"
两次?
为什么ng-controller
仅将提交按钮指示为表单字段(输入元素),也是表单的一部分?
为什么要Javascript
获取表单数据,因为您可以使用ng-model
或其他角度directives
在控制器和视图之间进行通信?
<强>样本强>
var myApp = angular.module('phase2',[]);
myApp.controller('SignUP', function($scope) {
$scope.save = function(selectedUser) {
alert(selectedUser);
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="phase2" ng-controller="SignUP">
<form name="myForm" ng-submit="save(uType)">
<p>Choose User Type :<br>Student<input type="radio" name="UserType" value="s" ng-model="uType"/>Teacher<input type="radio" name="UserType" value="t" ng-model="uType"/></p>
<input type="submit" name="signup" value="SignUP"/>
</form>
</div>