admin管理员组文章数量:1355748
So I can not make the input into a integer. I want to turn this into a int so then it can calculate the age of you. I have been researching but can not find the right answer. HTML CODE:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>learn</title>
<script src="script.js"></script>
</head>
<form action="/learn.html">
<b>Year Born:</b> <input id="input988744" type="value">
</form>
<button onClick="calculateAge(birthYear);">Summit</button>
<body>
</body>
</html>
Javascript Code:
/*
Function
*/
// var
var birthYear;
birthYear = document.getElementById("input988744");
// function
function calculateAge(birthYearInFun) {
"use strict";
console.log(2018 - birthYearInFun); // make it a number
}
So I can not make the input into a integer. I want to turn this into a int so then it can calculate the age of you. I have been researching but can not find the right answer. HTML CODE:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>learn</title>
<script src="script.js"></script>
</head>
<form action="/learn.html">
<b>Year Born:</b> <input id="input988744" type="value">
</form>
<button onClick="calculateAge(birthYear);">Summit</button>
<body>
</body>
</html>
Javascript Code:
/*
Function
*/
// var
var birthYear;
birthYear = document.getElementById("input988744");
// function
function calculateAge(birthYearInFun) {
"use strict";
console.log(2018 - birthYearInFun); // make it a number
}
Share
Improve this question
asked Oct 28, 2018 at 1:02
George DavisGeorge Davis
191 gold badge2 silver badges6 bronze badges
1
- On what line? @Mirodinho – George Davis Commented Oct 28, 2018 at 1:10
3 Answers
Reset to default 42 things needs to be changed
HTML:Change type of
<input>
fromtext
tonumber
, number can be negative so can add parametermin="1900"
to stop from entering date earlier then 1900<b>Year Born:</b> <input id="input988744" type="number" min="1900">
Javascript: Need to fetch value of HTML element, thus add
.value
adtergetElementById
, asgetElementById
gives HTML element not its value. To be on safer side useparseInt()
orNumber()
overbirthYear
var birthYear; birthYear = Number(document.getElementById("input988744").value); or birthYear = parseInt(document.getElementById("input988744").value);
Both these will make birthYear number.
What you really want it looks like is the input type to be a date... Check out the snippet below
legend {
background-color: #000;
color: #fff;
padding: 3px 6px;
}
.output {
font: 1rem 'Fira Sans', sans-serif;
}
input {
margin: .4rem;
}
label {
display: inline-block;
text-align: right;
width: 20%;
}
<fieldset>
<legend>Choose trip dates</legend>
<div>
<label for="start">Start</label>
<input type="date" id="start" name="trip"
value="2018-07-22"
min="2018-01-01" max="2018-12-31" />
</div>
<div>
<label for="end">End</label>
<input type="date" id="end" name="trip"
value="2018-07-29"
min="2018-01-01" max="2018-12-31"/ >
</div>
</fieldset>
You can access the value of the input field as birthYearInFun.value as Mirodinho said and then if it a string, convert it to a number using parseInt().
birthYear = parseInt(document.getElementById("input988744").value)
本文标签: javascriptHow to make a input in HTML in JS to integerStack Overflow
版权声明:本文标题:javascript - How to make a input in HTML in JS to integer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744035977a2579774.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论