[英]how can i make my javascript to read the form value?
i'm trying to do a form that will allow the user to see the details that he put into the fields after he clicked the button. 我正在尝试做一个表格,允许用户单击按钮后看到他在字段中输入的详细信息。 for some reason the javascript doesn't read the values of the text the user put into the form.
由于某些原因,javascript无法读取用户放入表单中的文本的值。 any help?
有什么帮助吗?
<form>
first name:
<input type="text" autofocus autocomplete="on" placeholder="first name" required id="first">
last name:
<input type="text" autocomplete="on" placeholder="last name" required id="last">
year of birth:
<input type="number" pattern="^[0-9]{4}" id="year">
gender:
<select id="sel">
<option selected>MALE</option>
<option>FEMALE</option>
</select>
<button id="butt">click</button>
</form>
the javascript : javascript:
function cont(){
function person()
{
this.FirstName = null;
this.LastName = null;
this.YearOfBirth = null;
this.Gender = null;
this.Weight = null;
this.Height = null;
this.Country = null;
this.FullName = function()
{
return this.FirstName + " " + this.LastName
};
this.Age = function()
{
var today = new Date();
var yy = today.getFullYear();
return yy - this.YearOfBirth;
};
this.toString = function()
{
return "this rider lives in " + this.Country + " and his name is " + this.FirstName + " " + this.LastName;
};
}
var rider = new person
rider.FirstName = document.getElementById('first').value
rider.LastName = document.getElementById('last').value
rider.YearOfBirth = document.getElementById('year').value
rider.Gender = document.getElementById('sel').value
document.getElementById('butt').onclick = function()
{
if (rider.FirstName != typeof ("hello") || rider.LastName != typeof("hi") || rider.YearOfBirth == isNaN)
{
alert ("fill all the required fields")
}
else
{
document.write(rider.FirstName + "<br>")
document.write(rider.LastName + "<br>")
document.write(rider.YearOfBirth + "<br>")
document.write(rider.Gender + "<br>")
document.write(rider.FullName() + "<br>")
}
} }
You are setting the "rider." 您正在设置“骑手”。 values when the page loads (and they are empty) but not getting them again when the user clicks the button.
页面加载时的值(它们为空),但是当用户单击按钮时不再获取它们。 Thus, the
document.write
functions are just writing empty values. 因此,
document.write
函数只是写入空值。 Try: 尝试:
document.getElementById('butt').onclick = function()
{
rider.FirstName = document.getElementById('first').value;
rider.LastName = document.getElementById('last').value;
rider.YearOfBirth = document.getElementById('year').value;
rider.Gender = document.getElementById('sel').value;
if (rider.FirstName != typeof ("hello") || rider.LastName != typeof("hi") || rider.YearOfBirth == isNaN)
{
alert ("fill all the required fields");
}
else
{
document.write(rider.FirstName + "<br>");
document.write(rider.LastName + "<br>");
document.write(rider.YearOfBirth + "<br>");
document.write(rider.Gender + "<br>");
document.write(rider.FullName() + "<br>");
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.