你可以在脚本中找到错误......我没有得到结果显示... [英] Can u find the error in the script......I am not getting the result being displayed...

查看:72
本文介绍了你可以在脚本中找到错误......我没有得到结果显示...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script type="text/javascript">
function calculate()
{
	var num1=parseInt(document.getElementById("litres").value);
	var num2=parseInt(document.getElementById("fat").value);
	var num3=num1+num2;
	document.getElementById("total").innerHTML=num3;
}
</script>




Customer Id	:<input type="number" name="cid" size="5"><br>
No.Of .Litres	:<input type="number" id="litres" name="litres" size="5"><br>
Fat   		:<input type="number" id="fat" name="fat"size="5"><br>
Total		:<input type="number" id="total" size="5"><br>
<input type="submit" value="Submit" >		<input type="reset" value="Reset">

推荐答案

代码中有更多错误代码:-)

HTML中没有任何内容可以调用任何函数。



首先,致命的错误:没有什么叫计算。您需要将 id 添加到您的按钮,例如 id =button然后您可以写,最简单的情况:

There are more bugs then code in your code :-)
Nothing in your HTML calls any functions.

First, fatal bugs: nothing calls calculate. You need to add id to your button, say id="button" and then you could write, in simplest case:
var button = document.getElementById("button");
button.onclick = calculate;





另一个错误是分配给 innerHTML 在你的最后一行。它应该是 val



在这两个修复之后,脚本将立即运行。之后,我建议你以某种合理的方式重写代码。首先,您不需要提交输入;只有在您拥有服务器部分并且想要执行HTTP请求时才需要表单。在纯JavaScript代码中,使用通常的按钮



不要使用通过<$获得的对象c $ c> document.getElementById 立即。返回对象然后保留以供重用。使用一些初始值初始化控件。



您的总计字段不必是输入。实际上可以在其中输入一些数据令人困惑。考虑使用任何只读元素,例如 p div span - 任何东西(而不是你真正使用 innerHTML )。或至少使其成为只读输入。请注意,在桌面应用程序中,此类仅显示只读输入文本框对于允许用户将其内容放入剪贴板非常重要,但使用HTML时,其他元素始终可以使用,而不仅仅是输入元素。



-SA



Another bug is assignment to innerHTML in your last line. It should be val instead.

After these two fixes, the script will work immediately. After that, I suggest you rewrite the code the way in some reasonable ways. First of all, you don't need submit input; it is only needed for forms, only if you have the server part and want to perform HTTP request. In your pure-JavaScript code, use a usual button.

Don't use objects obtained via document.getElementById immediately. Return the objects and keep then for reuse. Initialize controls with some initial values.

Your "total" field doesn't have to be input. It's confusing that one actually can enter some data in it. Consider using any read-only element, such as p, div, span — anything (and than you would really use innerHTML). Or at least make it read-only input. Note that in desktop applications such "display-only" read-only input text boxes can be important to allow the users to put their content in the clipboard, but with HTML it's always possible for other elements, not only input ones.

—SA


这篇关于你可以在脚本中找到错误......我没有得到结果显示...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆