如何使用输入标记获取用户输入并使用javascript返回值?

如何使用输入标记获取用户输入并使用javascript返回值?,javascript,html,Javascript,Html,我正在尝试制作一个预算计算器,它接受用户对不同变量的输入,并在计算后返回一个新值 我尝试使用一个输入标记来检索不同变量的值,并使用一个javascript函数来计算结果,该函数将结果显示为另一个输入标记的值。我是编程新手,所以我不确定这样做是否正确 var公司、租金、vloan、hins、VIN、cc、贷款、食品、天然气、娱乐、支出、结果; 函数计算(){ inc=document.getElementById(“收入”).值; 租金=document.getElementById(“租金”)

我正在尝试制作一个预算计算器,它接受用户对不同变量的输入,并在计算后返回一个新值

我尝试使用一个输入标记来检索不同变量的值,并使用一个javascript函数来计算结果,该函数将结果显示为另一个输入标记的值。我是编程新手,所以我不确定这样做是否正确

var公司、租金、vloan、hins、VIN、cc、贷款、食品、天然气、娱乐、支出、结果;
函数计算(){
inc=document.getElementById(“收入”).值;
租金=document.getElementById(“租金”).value;
vloan=document.getElementById(“vloan”).value;
hins=document.getElementById(“hinsurance”).value;
vins=document.getElementById(“vinsurance”).value;
cc=document.getElementById(“信用卡”).值;
loan=document.getElementById(“loan”).value;
food=document.getElementById(“food”).value;
气体=document.getElementById(“气体”).value;
娱乐=getElementById(“娱乐”)。值;
支出=getElementById(“支出”).value;
return document.getElementById(“result”).value=inc-rent-vloan-hins-vins-cc-loan-food-gas-entertainment-spending;
}

输入您的收入:

抵押/租金:

车辆贷款:

家庭保险:

车辆保险:

信用卡:

其他贷款:

杂货:

燃气/公共交通:

有线/互联网:

花钱:

算计 建议将你收入的20%拨入储蓄账户


输入值为字符串类型。在执行任何算术运算之前,必须将它们转换为数字。您可以使用
parseFloat()

您还忘记在以下语句中引用
文档
对象:

entertainment = getElementById("entertainment").value;
spending = getElementById("spending").value;
您也不需要从函数返回任何内容

工作代码示例:

var公司、租金、vloan、hins、VIN、cc、贷款、食品、天然气、娱乐、支出、结果;
函数计算(){
inc=parseFloat(document.getElementById(“收入”).value);
租金=parseFloat(document.getElementById(“租金”).value);
vloan=parseFloat(document.getElementById(“vloan”).value);
hins=parseFloat(document.getElementById(“hinsurance”).value);
vins=parseFloat(document.getElementById(“vinsurance”).value);
cc=parseFloat(document.getElementById(“creditcard”).value);
loan=parseFloat(document.getElementById(“loan”).value);
food=parseFloat(document.getElementById(“food”).value);
gas=parseFloat(document.getElementById(“gas”).value);
entertainment=parseFloat(document.getElementById(“entertainment”).value);
支出=parseFloat(document.getElementById(“支出”).value);
document.getElementById(“结果”).value=inc-rent-vloan-hins-vins-cc-loan-food-gas-entertainment-expensation;
}

输入您的收入:

抵押/租金:

车辆贷款:

家庭保险:

车辆保险:

信用卡:

其他贷款:

杂货:

燃气/公共交通:

有线/互联网:

花钱:

算计 建议将你收入的20%拨入储蓄账户


您忘记添加文档。

之前:

 entertainment = getElementById("entertainment").value;
 spending = getElementById("spending").value;
现在:(换成这个)

 entertainment = getElementById("entertainment").value;
 spending = getElementById("spending").value;
entertainment = document.getElementById("entertainment").value;
spending = document.getElementById("spending").value;