html 和 javascript 中的工资计算器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13928674/
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
Salary calculator in html and javascript
提问by Narayan Subedi
<html>
<head>
<script>
function calculate(){
var a = parseFloat(frmMain.name.value);
var b = parseFloat(frmMain.Salary.value);
var c = parseFloat(frmMain.taxrate.value);
}
</script>
</head>
<body>
<form name="frmMain">
Name <input type ="text" id="name" /><br />
Salary <input type ="text" id="Salary" /><br />
tax rate <input type ="text" id="taxrate" /><br />
<input type="button" value="calculate" onclick="calculate()" />
<input type="reset" value="Clear" /><br />
Sammary<textarea cols="20" rows="5" ></textarea>
</form>
</body>
</html>
i try to make it like this but i dont know how to get the salary and the name and taxrate put it in the comment or notes place may any body help ? the program should get the name and taxrate and salary and print it inside the textarea using javascript when i click on calculate ? i dont have an idea how to do this
我尝试这样做,但我不知道如何获得薪水以及姓名和税率,将其放在评论或备注处,任何人都可以帮忙吗?当我点击计算时,程序应该获取姓名、税率和工资,并使用 javascript 将其打印在 textarea 中?我不知道如何做到这一点
回答by Henrik Andersson
What you want is to add an id to the textarea, maybe id="texty"
after that you can get that elemnt with javascript, like this, var area = document.getElementById('texty');
and from there you use the .innerHTML
attribute to set its value.
您想要的是向 textarea 添加一个 id,也许id="texty"
之后您可以使用 javascript 获取该元素,就像这样,var area = document.getElementById('texty');
然后您可以使用该.innerHTML
属性来设置其值。
area.innerHTML = b*c;
<- this math isn't what you're after but I used a simple case to show you how its done! :)
area.innerHTML = b*c;
<- 这个数学不是你想要的,但我用一个简单的案例向你展示它是如何完成的!:)
If you're more keen on using value
then read this postto understand the diffrences between .innerHTML
and .value
如果您更热衷于使用,请value
阅读这篇文章以了解.innerHTML
和.value
Also reading the JS docs on MDN is very good reference, I'll put a link in the bottom!
另外阅读 MDN 上的 JS 文档是非常好的参考,我会在底部放一个链接!
Check out the tinker below!
看看下面的修补匠!
回答by jaychapani
回答by Narayan Subedi
<html>
<head>
<script>
function calculate(){
var a = frmMain.name.value;
var b = parseFloat(frmMain.Salary.value);
var c = parseFloat(frmMain.taxrate.value);
var area = document.getElementById("text");
area.innerHTML='Name : '+a+'\nSalary : '+b+'\nTax Rate : '+c;
}
</script>
</head>
<body>
<form name="frmMain">
Name <input type ="text" id="name" /><br />
Salary <input type ="text" id="Salary" /><br />
tax rate <input type ="text" id="taxrate" /><br />
<input type="button" value="calculate" onclick="calculate()" />
<input type="reset" value="Clear" /><br />
Sammary<textarea cols="20" rows="5" id="text"></textarea>
</form>
</body>
</html>