简体   繁体   English

抓取表单字段并根据输入进行计算

[英]Grab form fields and calculate based on inputs

I'm getting an undefined error and don't know why this isn't working. 我收到一个undefined错误,不知道为什么这不起作用。

It's supposed to divide the rent by the amount of roommates: 应该将房租除以室友数:

 function splitRent() { var roommates = document.getElementById("rent"); var rent = document.getElementById("rent"); var rentEach = rent / roommates; if (document.getElementById("submit") == true) { document.write("You each should pay" + " " + rentEach) } else { document.alert("Gimme info") } }; 
 <h1>Roommate Room Splitter</h1> <form id="myForm"> Roommates: <input type="text" name="roommates" id="roommates"> <br/>Rent: <input type="text" name="rent" id="rent"> <br/> <input type='submit' id='submit' value='Submit' onclick="splitRent()" /> </form> 

You want to take the value of the fields, not the fields themselves. 您想获取字段的value ,而不是字段本身。

document.getElementById() returns the node, but you want the value of the input field: document.getElementById()返回节点,但是您需要input字段的value

var rent = document.getElementById("rent").value;

Also, you're getting the value of the rent twice; 另外,您将获得两次租金价值。 you want to check the roommates as well. 您也要检查室友。

var roommates = document.getElementById("roommates").value;

Lastly, document.getElementById("submit") == true doesn't mean anything: you're comparing a button node with a boolean value, which doesn't make sense. 最后, document.getElementById("submit") == true并不意味着任何事情:您正在将按钮节点与布尔值进行比较,这没有任何意义。 If you want to check to make sure that both fields are filled, try this: 如果要检查以确保两个字段均已填写,请尝试以下操作:

if(roommates && rent){
    //do calculations
}else{
   window.alert("Enter something"); //note that it's window.alert(), not document.alert(), which is not a function

As it stands, this allows people to enter things that are not numbers; 就目前而言,这使人们可以输入非数字的内容。 there are two things that you could do to fix that. 您可以通过两件事来解决该问题。

  1. Use parseInt() / parseFloat() to ensure that you're extracting a number 使用parseInt() / parseFloat()确保您提取的是数字
  2. Check that you actually have a number before doing calculations 计算前检查您是否确实有数字

You'd do something like this: 您将执行以下操作:

var rent = parseInt(document.getElementById("rent").value);
var roommates = parseFloat(document.getElementById("rooommates").value);

If you use the checking I've done above ( rent && roommates ), the validation will take place there (it checks for both empty and NaN values). 如果您使用我在上面所做的检查( rent && roommates ),验证将在此处进行(检查空值和NaN值)。

 function splitRent() { var roommates = parseInt(document.getElementById("roommates").value); var rent = parseFloat(document.getElementById("rent").value); var rentEach = rent / roommates; if (roommates && rent) { document.write("You each should pay" + " " + rentEach) } else { window.alert("Gimme info") } }; 
 <h1>Roommate Room Splitter</h1> <form id="myForm"> Roommates: <input type="text" name="roommates" id="roommates"> <br/>Rent: <input type="text" name="rent" id="rent"> <br/> <input type='submit' id='submit' value='Submit' onclick="splitRent()" /> </form> 

Shouldn't this be: 这不应该是:

    var roommates = document.getElementById("roommates").value;
    var rent = document.getElementById("rent").value;

Do you always get "1" for your result? 您是否总是将结果设为“ 1”?

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

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