简体   繁体   English

简单的Javascript矩形区域功能

[英]Simple Javascript rectangle area function

I'm writing a simple function to calculate the area of a rectangle... 我正在编写一个简单的函数来计算矩形的面积...

Here is my code: 这是我的代码:

<form id="rectangleForm" method="post">
<h1>Calculate the area of a rectangle:</h1>
<div>
    <label for="width">Width</label> 
    <input type="text" name="width" id="width" value="1.00" />
</div>
<div>
    <label for="height">Height</label>
    <input type="text" name="height" id="height" value="1.00"/>
</div>
<div>
    <label for="area">Area</label>
    <input type="text" name="area" id="rectarea" value="0.00" />
</div>
<div>
    <input type="submit" value="Calculate" id="submit" />
</div>

</form>
<script>

function rectangle() {
'use strict';
var area;
var width = document.getElementById('width').value;
var height = document.getElementById('height').value;
total = width * height;
document.getElementById('area').value = total;
return false;
}          

function init() {
   'use strict';
   var rectangleForm = document.getElementById('rectangleForm');
   rectangleForm.onsubmit = rectangle();
}

window.onload = init();
</script>

I'm getting an "uncaught typeError: cannot read the property 'value' of null." 我收到“未捕获的typeError:无法读取null的属性'value'。” error on the var width line. 宽度线错误。 I don't understand why. 我不明白为什么。 Can anyone shed any details or solutions? 任何人都可以提出任何细节或解决方案吗?

There are three problems in your code. 您的代码中存在三个问题。

Firstly, on the following lines: 首先,在以下几行中:

rectangleForm.onsubmit = rectangle();

window.onload = init();

...you are calling the rectangle() and init() functions and assigning their results as the onsubmit and onload handler, which means the functions are run once straight away and not run in response to the form submission event. ...您正在调用 rectangle()init()函数,并将其结果分配为onsubmitonload处理程序,这意味着这些函数将立即运行一次,而不响应表单提交事件而运行。 You need to remove the parentheses so that the functions themselves get assigned as handlers: 您需要删除括号,以便将函数本身分配为处理程序:

rectangleForm.onsubmit = rectangle;

window.onload = init;

Secondly, your area input has id="rectarea" but in your code you use document.getElementById("area") - you need to match these up. 其次,您的区域输入具有id="rectarea"但是在您的代码中使用document.getElementById("area") -您需要将它们匹配。

Thirdly, your functions have the 'use strict'; 第三,您的职能具有'use strict'; directive, but then your code isn't actually strict because you don't declare the total variable. 指令,但是您的代码实际上并不严格,因为您没有声明total变量。 So change: 所以改变:

total = width * height;

to: 至:

var total = width * height;

With those changes applied your code works as shown in this demo: http://jsbin.com/avimac/1/edit 应用这些更改后,您的代码将如本演示所示进行工作: http : //jsbin.com/avimac/1/edit

Here is a working fiddle of your script: 这是您的脚本的有效提要:

http://jsfiddle.net/sBRr7/2/ http://jsfiddle.net/sBRr7/2/

EDITED: Based on the additional comments by nnnnnn I updated mine aswell (although his answer is more explanatory) 编辑:基于nnnnnn的其他评论,我也更新了我的(尽管他的回答更具解释性)

Removed the post: 删除了该帖子:

<form id="rectangleForm">

Fixed the id of the input: 修复了输入的ID:

<input type="text" name="area" id="area" value="0.00" />

Changed the input type to button: 将输入类型更改为按钮:

<input type="button" value="Calculate" id="submit" />

Added variable declaration: 添加了变量声明:

var total = width * height; 

Changed the init function: 更改了init函数:

    var submit = document.getElementById('submit');
    submit.onclick = rectangle;

Changed the onload handler: 更改了onload处理程序:

window.onload = init;

First the obvious error here is that u have to assign a reference to onload and onsubmit functions and not execute the functions. 首先,这里明显的错误是,您必须为onload和onsubmit函数分配一个引用,而不执行这些函数。 Like this... 像这样...

windows.onload = init;

And also ... 并且 ...

rectangleFom.onSubmit = rectangle;

Try after fixing these errors. 解决这些错误后,请尝试。

Only 3 things to fix in your code, working copy here: http://tests.dev.activisual.net/test2.html 您的代码中仅可修复3件事,可在此处进行工作复制: http : //tests.dev.activisual.net/test2.html

  1. Remove 'Use String'; 删除“使用字符串”; That does nothing and in chrome it crashes the code. 那什么也没做,在chrome中它使代码崩溃。
  2. Don't call the function before assigning it to the event (because the function gets executed instead of assigned), so instead of rectangleForm.onsubmit = rectangle(); 在将其分配给事件之前不要调用该函数(因为该函数将被执行而不是被分配),所以请不要使用rectangleForm.onsubmit = rectangle(); use rectangleForm.onsubmit = rectangle; 使用rectangleForm.onsubmit = rectangle;
  3. Be more carefull when calling your ids. 呼叫ID时请多加注意。 Your result box is not called "area" is called "rectarea", so change getElementById('area') to getElementById('rectarea') . 您的结果框未称为“区域”,称为“ rectarea”,因此将getElementById('area')更改为getElementById('rectarea')

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

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