代码应该做什么:
以下是代码:
<script type="text/javascript">
var myObject = {
myFunction: function(){
return document.getElementById("carDebt");
},
h: function(){
var carLoan=myFunction();
var RATE12 = 0.005;
var TIMERATE = 0.25862780376;
return Math.round((carLoan * RATE12) / TIMERATE);
}
writeIt: function(){
var g = myObject.h();
var xyz = g;
var abc = 2;
var efg = 3;
var somearray = [xyz,abc,efg];
var z = 0;
for (i=0; i<somearray.length; i++) {
z += somearray[i];
};
document.getElementById("result").innerHTML=z;
}
};
</script>
<body>
<form>
Amt Due on Car Loan: <input type="number" id="carDebt">
</form>
<form>
<input type="button" onclick="myObject.writeIt()" value="Click here when done" id="button1">
</form>
<p id="result">Results Here</p>
</body>
我没有得到任何东西,就像在,甚至没有NaN或未定义。我可能遗漏了一些显而易见的东西,但我尝试了千种方法!
答案 0 :(得分:0)
这一行可能是罪魁祸首:
var carLoan=myFunction();
尝试引用它下面的对象:
var carLoan = myObject.myFunction();
此外,该函数返回DOM元素而不是DOM元素的值。您可能希望编辑该函数以返回值:
myFunction: function(){
return document.getElementById("carDebt").value;
}
答案 1 :(得分:0)
我还注意到你的标签太多了。你的意思是有两个“形式”吗?
答案 2 :(得分:0)
似乎答案已经在上面拼凑起来了,即修复拼写错误,得到的价值不是元素,有一个&#34;结果&#34;要输出的元素。我还添加了#34;这个&#34;到myFunction调用。 我的版本:
<script type="text/javascript">
var myObject =
{
myFunction: function()
{
return document.getElementById("carDebt").value;
},
h: function()
{
var carLoan = this.myFunction();
var RATE12 = 0.005;
var TIMERATE = 0.25862780376;
return Math.round((carLoan * RATE12) / TIMERATE);
},
writeIt: function()
{
var g = myObject.h();
var xyz = g;
var abc = 2;
var efg = 3;
var somearray = [xyz,abc,efg];
var z = 0;
for(i=0; i<somearray.length; i++)
{
z += somearray[i];
};
document.getElementById("result").innerHTML=z;
}
};
</script>
</head>
<body>
<form> Amt Due on Car Loan: <input type="number" id="carDebt"> </form>
<form> <input type="button" onclick="myObject.writeIt()" value="Click here when done" id="button1"> </form>
<div><p id="result"></p></div>
</body>