2013-08-28 54 views
0

我需要确保最小答案为5和最大不能超过25。我使用的代码是较大:JavaScript设置最大最小&问答

function temp(form) 
{ 
    var i = parseFloat(form.Inc.value, 10); 
    var c = 0; 
    c = 25 - ((300 - i) * 8.0/100); 
    form.Fee.value = c.toFixed(2); 

    function decrease(form) 
    { 
     if (c > 25) 
     { 
      c--; 
      document.getElementById('Fee').innerHTML = 25; 
     } 
    } 

    function increase(form) 
    { 
     if (c < 5) 
     { 
      c++; 
      document.getElementById('Fee').innerHTML = 5; 
     } 
    } 
} 

然而,在形式回答框没有按”不承认最低和最高数字。

+0

你应该添加一些更多的解释。就目前而言,我几乎没有线索,这段代码打算做什么。 – Sirko

+0

对不起,我没有意识到。 – GregSmith

+0

@Sirko代码使人们可以将值(i)放入表单中,然后计算答案。等式的fo = rmula为:价值(i)为每周收入金额,答案为费用(c)。 – GregSmith

回答

0

form.Fee.value意味着你的表单有一个名为“Fee”的元素。
The "name" attribute is different from the "id" attribute,并且由于您可能未给出表单元素的id,因此document.getElementById()大概会返回null。
考虑使用document.getElementsByName或使用form.Fee。

此外,reduce()和increase()方法未被调用。
我认为你正在寻找这样的代码:

function temp(form) 
{ 
    var i = parseFloat(form.Inc.value, 10); 
    var c = 0; 
    c = 25 - ((300 - i) * 8.0/100); 

    // Alternatively use Math.min and Math.max 
    if (c > 25) c = 25; 
    if (c < 5) c = 5; 

    form.Fee.value = c.toFixed(2); 
} 

其他注意事项; var c可以用它下面的表达式进行初始化;初始化为0是不必要的。

相关问题