如何从角度Js中的html中的单选按钮获取选定值

时间:2017-04-15 19:36:07

标签: javascript html angularjs radio-button

我尝试从单选按钮中获取所选值 但是,当我选择老师时,它仍然是学生"

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;
&#13;
&#13;

RigesterationController.js文件:

&#13;
&#13;
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;
&#13;
&#13;

尽管我选择老师,但我总能得到...如何获得选定的价值? 提前谢谢

1 个答案:

答案 0 :(得分:1)

一些建议:

  • 为什么ng-app="phase2"两次?

    • ng-app指令用于自动引导AngularJS应用程序。根据AngularJS文档,每个HTML文档只能自动引导一个AngularJS应用程序。
  • 为什么ng-controller仅将提交按钮指示为表单字段(输入元素),也是表单的一部分?

    • 它应该出现在父元素上。
  • 为什么要Javascript获取表单数据,因为您可以使用ng-model或其他角度directives在控制器和视图之间进行通信?

    • 您可以使用ng-model将表单数据传递到控制器中,因为它是AngularJS中非常流行且功能强大的功能,而不是以错误的方式执行。

<强>样本

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>