简体   繁体   English

无法使用getElementByID从表单添加元素

[英]can't add elements from form, using getElementByID

I can't seem to get my JavaScript to add the elements from my HTML page. 我似乎无法获得JavaScript来添加HTML页面中的元素。 Do I have a syntax error? 我有语法错误吗?

 var mondayHours = document.getElementById("mondayHours").value; var tuesdayHours = document.getElementById("tuesdayHours").value; var wednesdayHours = document.getElementById("wednesdayHours").value; var thursdayHours = document.getElementById("thursdayHours").value; var fridayHours = document.getElementById("fridayHours").value; var saturdayHours = document.getElementById("saturdayHours").value; var sundayHours = document.getElementById("sundayHours").value; var totalHours = mondayHours + tuesdayHours + wednesdayHours + thursdayHours + fridayHours + saturdayHours + sundayHours; function alertHours() { alert(totalHours); } 
 <fieldset> <p>Hours of Operation</p> <p> <label for="mondayHours">Monday <input name="mondayHours" type="number" id="mondayHours" /> </label> </p> <p> <label for="tuesdayHours">Tuesday <input name="tuesdayHours" type="number" id="tuesdayHours" /> </label> </p> <p> <label for="wednesdayHours">Wednesday <input name="wednesdayHours" type="number" id="wednesdayHours" /> </label> </p> <p> <label for="thursdayHours">Thursday <input name="thursdayHours" type="number" id="thursdayHours" /> </label> </p> <p> <label for="fridayHours">Friday <input name="fridayHours" type="number" id="fridayHours" /> </label> </p> <p> <label for="saturdayHours">Saturday <input name="saturdayHours" type="number" id="saturdayHours" /> </label> </p> <p> <label for="sundayHours">Sunday <input name="sundayHours" type="number" id="sundayHours" /> </label> </p> </fieldset> <input name="Calculate" type="submit" value="submit" onclick="alertHours()" /> <script src="Calculator_script.js"></script> 

You have to retrieve form data when you called your alertHours function. 调用alertHours函数时,必须检索表单数据。

The problem was that you retrieved form data at the beginning, and these data were undefined. 问题是您在一开始就检索了表单数据,而这些数据是未定义的。

JS JS

function alertHours(){
  var mondayHours = parseFloat(document.getElementById("mondayHours").value) || 0;
  var tuesdayHours = parseFloat(document.getElementById("tuesdayHours").value) || 0;
  var wednesdayHours = parseFloat(document.getElementById("wednesdayHours").value) || 0;
  var thursdayHours = parseFloat(document.getElementById("thursdayHours").value ) || 0;
  var fridayHours = parseFloat(document.getElementById("fridayHours").value) || 0;
  var saturdayHours = parseFloat(document.getElementById("saturdayHours").value ) || 0;
  var sundayHours = parseFloat(document.getElementById("sundayHours").value) || 0;

  var totalHours = mondayHours + tuesdayHours + wednesdayHours + thursdayHours + fridayHours + saturdayHours + sundayHours;

  alert(totalHours)
} 

When you see parseFloat(...) || 0 当您看到parseFloat(...) || 0 parseFloat(...) || 0 , this tell : ok if an input is empty, i will set the 0 value for this input. parseFloat(...) || 0 ,表明:如果输入为空,我将为此输入设置0值。

You are grabbing the values on page load and storing those in variables. 您正在获取页面加载时的值并将其存储在变量中。 Since at that time, they are valueless, you aren't getting your expected result. 由于那时它们是无价的,所以您没有得到预期的结果。 I would recommend the following option... 我建议以下选项...

var mondayHours = document.getElementById("mondayHours");
var tuesdayHours = document.getElementById("tuesdayHours");
var wednesdayHours = document.getElementById("wednesdayHours");
var thursdayHours = document.getElementById("thursdayHours");
var fridayHours = document.getElementById("fridayHours");
var saturdayHours = document.getElementById("saturdayHours");
var sundayHours = document.getElementById("sundayHours");

function alertHours() {
  var totalHours = mondayHours.value + tuesdayHours.value + wednesdayHours.value + thursdayHours.value + fridayHours.value + saturdayHours.value + sundayHours.value;
  alert(totalHours);
}

This way you are getting the values of the inputs at the time the function is invoked, and not the time that the page was loaded. 这样,您将在调用函数时而不是在页面加载时获取输入值。 And scoping the getElementById outside of the alertHours function still gives you access to those elements should you need to use them somewhere else in code. 而且,如果需要在代码中的其他地方使用它们,则在alertHours函数之外对getElementById进行作用域设置仍然可以使您访问这些元素。 There are more clever/eloquent ways to do this still, but this should work. 还有更多聪明/雄辩的方法可以做到这一点,但这应该行得通。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM