如何使javascript读取表单值?
我正在尝试创建一个表单,允许用户在单击按钮后查看他输入到字段中的详细信息。出于某种原因,javascript不会读取用户输入表单中的文本值。有什么帮助吗如何使javascript读取表单值?,javascript,html,forms,Javascript,Html,Forms,我正在尝试创建一个表单,允许用户在单击按钮后查看他输入到字段中的详细信息。出于某种原因,javascript不会读取用户输入表单中的文本值。有什么帮助吗 <form> first name: <input type="text" autofocus autocomplete="on" placeholder="first name" required id="first"> last name: <input type="text" autocomplete="o
<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>
名字:
姓氏:
出生年份:
性别:
男性
女性
点击
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>")
}
} }
函数cont(){
职能人员()
{
this.FirstName=null;
this.LastName=null;
this.YearOfBirth=null;
这个。性别=空;
这个。权重=null;
此参数。高度=空;
this.Country=null;
this.FullName=函数()
{
返回this.FirstName+“”+this.LastName
};
this.Age=function()
{
var today=新日期();
var yy=today.getFullYear();
返回yy-本年出生;
};
this.toString=函数()
{
return“该骑手居住在”+this.Country+,他的名字是“+this.FirstName+”+this.LastName;
};
}
var rider=新人
rider.FirstName=document.getElementById('first')。值
rider.LastName=document.getElementById('last')。值
rider.YearOfBirth=document.getElementById('year')。值
rider.Gender=document.getElementById('sel').value
document.getElementById('butt')。onclick=function()
{
if(rider.FirstName!=typeof(“hello”)| | rider.LastName!=typeof(“hi”)| | rider.YearOfBirth==isNaN)
{
警报(“填写所有必填字段”)
}
其他的
{
文档。写入(附加说明:FirstName+“
”)
文档。写入(rider.LastName+“
”)
文件。书写(附文:出生年月+“
”)
文件。书写(骑手性别+“
”)
document.write(rider.FullName()+“
”)
}
} }
当页面加载(且为空)时,您正在设置“rider.”值,但当用户单击按钮时,不会再次获取这些值。因此,document.write
函数只是在写空值。尝试:
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>");
}
} }
document.getElementById('butt')。onclick=function()
{
rider.FirstName=document.getElementById('first')。值;
rider.LastName=document.getElementById('last')。值;
rider.YearOfBirth=document.getElementById('year')。值;
rider.Gender=document.getElementById('sel').value;
if(rider.FirstName!=typeof(“hello”)| | rider.LastName!=typeof(“hi”)| | rider.YearOfBirth==isNaN)
{
警报(“填写所有必填字段”);
}
其他的
{
文件。书写(rider.FirstName+“
”);
文件。书写(rider.LastName+“
”;
文件。书写(附文:出生年月+“
”;
文件。书写(rider.Gender+“
”);
document.write(rider.FullName()+“
”);
}
} }
JS代码何时何地针对表单执行?JS是如何触发的?你应该以结束你的行代码>。哪里定义了person
?我刚刚添加了所有脚本。泰:)