Javascript 如何使用JS和提交按钮计算两个输入表单字段并将值放入另一个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13627701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate two input form fields and put the value in another using JS and submit button?
提问by Alex
I know this is an extremely basic question, but I just couldn't find anything useful on the internet. I have 2 input boxes and I want calculate that input and put the result in output using submit button and navigating by form id.
我知道这是一个非常基本的问题,但我在互联网上找不到任何有用的东西。我有 2 个输入框,我想计算该输入并将结果放入输出中,使用提交按钮并按表单 ID 导航。
<script>
function Calculate()
{
var resources = GetFieldValue( "Resources" );
var minutes = GetFieldValue( "Minutes" );
var permin = parseFloat(resources) / 60;
var result = parseFloat(permin) * parseFloat(minutes);
</script>
回答by Dineshkani
<body>
<input type='text' id='Resources'/>
<input type='text' id='Minutes' onblur='Calculate();'/>
<form name ="testarea" Method="Get" Action="youpage.html" id='form1'>
<input type='text' id='answer' name='ans' />
</form>
</body>
<script>
function Calculate()
{
var resources = document.getElementById('Resources').value;
var minutes = document.getElementById('Minutes').value;
var permin = parseFloat(resources) / 60;
document.getElementById('answer').value=parseFloat(permin) * parseFloat(minutes);
document.form1.submit();
}
</script>
Your form will submit and your answer is in the url like
您的表单将提交,您的答案在网址中,例如
youpage.html?ans=youranswer
回答by Adder
Use the HTML button tag : http://www.w3schools.com/tags/tag_button.asp
使用 HTML 按钮标签:http: //www.w3schools.com/tags/tag_button.asp
<button onClick="Calculate();" value="calculate">
in the function Calculate instead of GetFieldValue use
在函数计算而不是 GetFieldValue 中使用
document.getElementbyId("Resources").value;
where
在哪里
<input type="text" id="Resources">
use
用
document.getElementbyId("Result").value = result;
to fill in another input element.
填充另一个输入元素。

