JavaScript에서 변수로 제출을 클릭하면 양식 입력 값을 가져 오려고합니다.HTML 폼에서 JavaScript로 데이터 가져 오기
그러나 콘솔 로그가 변수를 비어있는 것으로 표시하기 때문에 나는별로 운이없는 것처럼 보입니다. 내가 ADDUSER()를 제거하고 또한 하단의 ADDUSER 함수에 false를 돌려 추가 할 필요가 있었기 때문에
HTML
<html>
<head>
<meta charset="UTF-8">
<title>Registration</title>
</head>
<body>
<fieldset><legend>Registration</legend>
<form action="#" method="post" id="theForm">
<label for="username">Username<input type="text" name="username" id="username"></label>
<label for="name">Name<input type="text" name="name" id="name"></label>
<label for="email">Email<input type="email" name="email" id="email"></label>
<label for="password">Password<input type="password" name="password" id="password"></label>
<label for="age">Age<input type="number" name="age" id="age"></label>
<input type="hidden" name="unique" id="unique">
<input type="submit" value="Register!" id="submit">
</form>
</fieldset>
<div id="output"></div>
<script src="js/process.js"></script>
</body>
</html>
JS
var output = document.getElementById('output');
function addUser() {
'use strict';
var username = document.getElementById('username');
var name = document.getElementById('name');
var email = document.getElementById('email');
var password = document.getElementById('password');
var age = document.getElementById('age');
console.log(username.value);
console.log(name.value);
console.log(password.value);
}
// Initial setup:
function init() {
'use strict';
document.getElementById('theForm').onsubmit = addUser();
} // End of init() function.
//On window load call init function
window.onload = init;
편집 그것은이었다. ADDUSER는 아무 것도 반환하지 않기 때문에
function init() {
'use strict';
document.getElementById('theForm').onsubmit = addUser();
}
에서
는
왜 head 및 body 태그 외부에 스크립트가 있습니까? 'body' 태그 바로 앞에서 이동하십시오. – karthikr
내 부분의 @karthikr 실수, 지금 업데이트 됨 :) – Adam91Holt