简体   繁体   English

我如何使我的JavaScript读取表单值?

[英]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.

相关问题 如何让我的 javascript 表单重置按钮工作? - How can I make my javascript form reset button work? 在发送我的表单之前,我如何使用 javascript 验证基于 javascript 的表单复选框 - how can i make a javascript based form checkbox validate with javascript before sending my form MVC:如何确保我的字段在编辑表单的视图内选择了值? - MVC: How can I make sure that my field have the value selected inside my view on the edit form? 如何使用 javascript 获得 {{form}} 值? - How can i get {{form}} value with javascript? 如何单击DIV使用javascript提交表单? - How can I make a click on a DIV submit my form using javascript? 如何修复正在声明但从未读取其值的 Javascript slider 按钮 function ? - How can I fix my Javascript slider button function which is being declared but its value is never read? 为什么我不能从我的表单中获取价值? (JavaScript) - Why can't I take value from my form? (JavaScript) 如何使用javascript在对象数组中使json对象的字段名称具有值? - How can I make my json object's field name a value with in an object array with javascript? 如何将我的 javascript 正确链接到我的 html 表单? - How can I correctly link my javascript to my html form? 如何在JavaScript / jQuery中将滑动手势读取为点击? - How can I make swipe gesture read as a click in JavaScript/jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM