获取用户输入以在javascript中执行计算

时间:2015-09-29 20:59:55

标签: javascript

我写了这段代码,但它没有用。我想使用用户输入的值来执行以下计算...



function calculateBasic()
		{
			var uservalue = form.size.value;
			var powerForOrganic = Math.pow(uservalue,1.05);
			var effortForOrganic = 2.4 * power;
			var powerTdevOrganic = Math.pow(effortForOrganic,0.38);
			var tdevForOrganic =  2.5 * powerTdevOrganic;
			var averageStaffForOrganic = effortForOrganic / tdevForOrganic;
			var productivityLevelForOrganic = averageStaffForOrganic / effortForOrganic;
			alert( "Effort :" + effortForOrganic + "TDEV :" + tdevForOrganic + "Average Staff :" + averageStaffForOrganic + "Productivity :" + productivityLevelForOrganic);
			
		}

<input type="text" id="size" placeholder="Enter the size in KLOC">
<input type="button" id="enterInfo" Value="Submit"  onclick="calculateBasic(this.form);">
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

你的专栏应该是

var uservalue = document.getElementById("size").value;

power也未定义。你可能意味着powerForOrganic,它是之前定义的那条线。

var effortForOrganic = 2.4 * powerForOrganic;

这是完整的代码

function calculateBasic() {
    var uservalue = Number(document.getElementById("size").value);
    var powerForOrganic = Number(Math.pow(uservalue,1.05));
    var effortForOrganic = Number(2.4 * powerForOrganic);
    var powerTdevOrganic = Number(Math.pow(effortForOrganic,0.38));
    var tdevForOrganic =  Number(2.5 * powerTdevOrganic);
    var averageStaffForOrganic = effortForOrganic / tdevForOrganic;
    var productivityLevelForOrganic = averageStaffForOrganic / effortForOrganic;
    alert( "Effort :" + effortForOrganic + "TDEV :" + tdevForOrganic + "Average Staff :" + averageStaffForOrganic + "Productivity :" + productivityLevelForOrganic);

    }

var button = document.getElementById("enterInfo");

button.addEventListener("click", function() {
    calculateBasic();
});

http://jsfiddle.net/739amacp/

答案 1 :(得分:0)

在第4行,当我认为您打算引用power时,您会引用powerForOrganic

答案 2 :(得分:0)

这让我想起了使用javascript编程的古老时代。它仍然有效。

1)您将表单作为参数传递给函数,但函数没有得到它。 2)您在函数内使用的某些变量未声明或可用。 3)如果你得到form的值为form..value,那么你应该给出该元素的名称。

function calculateBasic(form)
		{
			var uservalue = (form.size.value);
			var powerForOrganic = Math.pow(uservalue,1.05);
			var effortForOrganic = 2.4 * powerForOrganic;
			var powerTdevOrganic = Math.pow(effortForOrganic,0.38);
			var tdevForOrganic =  2.5 * powerTdevOrganic;
			var averageStaffForOrganic = effortForOrganic / tdevForOrganic;
			var productivityLevelForOrganic = averageStaffForOrganic / effortForOrganic;
			alert( "Effort :" + effortForOrganic + "TDEV :" + tdevForOrganic + "Average Staff :" + averageStaffForOrganic + "Productivity :" + productivityLevelForOrganic);
			
		}
<form>
<input type="text" name="size" id="size" placeholder="Enter the size in KLOC">
<input type="button" id="enterInfo" Value="Submit"  onclick="calculateBasic(this.form)">
    </form>