2017-04-15 69 views
0

獲得選擇從單選按鈕值從HTML我試圖讓從單選按鈕 選擇的值,但是當我選擇的老師仍然是的「學生」如何角的js

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="[email protected]"></script> 
 
    <script src="https://code.angularjs.org/1.2.28/angular-route.js"></script> 
 
\t <script src="RigesterationController.js"></script> 
 
</head> 
 
<body > 
 

 
<center> 
 

 
<p>Enter Your User Name : \t <input type="text" , name="UserName" id ="UName" required /> </p> 
 
<p>Enter Your Email : \t <input type="text" , name="Email" id ="email" required /> </p> 
 
<p>Enter Your Password : \t <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" > 
 
\t <div ng-controller="SignUP"> 
 
\t \t <input type="button" name="signup" value="SignUP" ng-click="save()" /> 
 
\t </div> 
 
</div> 
 
</center> 
 
</body> 
 
</html>

RigesterationController.js文件:

var app = angular.module("phase2" , []) 
 
app.controller("SignUP" ,function ($scope , $http , srvShareData , $location) 
 
\t \t { 
 
\t \t \t $scope.dataToShare = []; 
 
\t \t \t $scope.save = function() { 
 
\t \t \t \t var email= document.getElementById("email").value; 
 
\t \t \t \t var UName=document.getElementById("UName").value; 
 
\t \t \t \t var Pass=document.getElementById("Pass").value; 
 
\t \t \t \t var gender=document.getElementById("Gender").value; 
 
\t \t \t \t var UserType=document.getElementById("Utype").value; 
 
\t \t \t \t alert(UserType) 
 
\t \t \t \t $http.get('http://localhost:8090/SignUp/'+email+'/'+UName+'/'+Pass+'/'+gender+'/'+UserType) 
 
\t \t \t \t .then(function(response) 
 
\t \t \t \t \t { 
 
\t \t \t \t \t }); 
 
\t \t \t \t if (UserType=="t") 
 
\t \t \t \t { 
 
\t \t \t \t window.location.href="http://localhost:8060/TheAngular_Project/TeacherPage.html"; 
 
\t \t \t \t } 
 
\t \t \t \t else if (UserType=="s") 
 
\t \t \t \t { 
 
\t \t \t \t window.location.href="http://localhost:8060/TheAngular_Project/StudentPage.html"; 
 
\t \t \t \t } 
 
\t \t \t \t } 
 
\t \t });

我總是得到S ^雖然我選擇的老師..如何讓選擇的價值? 由於事先

+0

的document.getElementById( 「郵件」)。價值?使用ng-model的值代替 – Sajeetharan

+0

電子郵件和用戶名和密碼工作成功..單選按鈕問題@Sajeetharan –

+0

@AhmedHassan我建議你先閱讀角度。你做錯了基礎知識 – Sajeetharan

回答

1

幾點建議:

  • 爲什麼ng-app="phase2"兩次?

    • ng-app指令用於自動引導AngularJS應用程序。根據AngularJS文檔,每個HTML文檔只能自動引導一個AngularJS應用程序。
  • 爲什麼ng-controller指令只對提交按鈕,表單字段(輸入要素)也表單的一部分?

    • 它應該在父元素上。
  • 爲什麼平原Javascript得到的形式的數據作爲可以使用ng-model或其他角度directives到控制器之間進行通信,並查看?

    • 可以使用NG-模型到控制器,因爲它是在AngularJS而不是在錯誤的方式做的非常流行且強大的功能傳遞表單數據。

DEMO

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>