计算字段的警报框

时间:2013-01-21 19:23:33

标签: html alert

如果Answer_2字段中的数字大于10或小于-10,我怎么能弹出警告框?

Here's my example

Javascript:

    function CalculateIMSUB(Atext, Btext, form, val)
    {
    var A = eval(Atext);
    var B = eval(Btext);
    if (isNaN(A)) A = 0;
    if (isNaN(B)) B = 0;
    var answer = A - B;
    form.Answer.value = answer;    

    var diff = answer - val;
    if (diff == 0)
    form.Answer_2.value = 'ok';
    else if (diff < 0)
    form.Answer_2.value = diff;
    else
    form.Answer_2.value = '+' + diff;
    }

    function calculateAll() {
    var forms = document.getElementsByTagName("form");
    for(var i = 0; i < forms.length; i++ ) {
    CalculateIMSUB(forms[i].input_A.value, forms[i].input_B.value,forms[i], 96)                
    }
    }

HTML:

    <FORM NAME="Calculator" METHOD="GET">
    <P><INPUT TYPE=TEXT NAME="input_A" SIZE=10><INPUT TYPE=TEXT NAME="input_B" SIZE=10>  
    <INPUT TYPE="button" VALUE="+" name="subtractbutton" onclick="CalculateIMSUB
    (this.form.input_A.value, this.form.input_B.value, this.form, 96)">
    <INPUT TYPE=TEXT NAME="Answer" SIZE=12><tt>96</tt><INPUT TYPE=TEXT NAME="Answer_2"   
    SIZE=4></P></form>
    <input type="button" onclick="calculateAll()" value="Master calculation" />

非常感谢

1 个答案:

答案 0 :(得分:1)

根据我的理解,你需要做的只是在你计算的地方var diff

if(diff < -10 || diff > 10)
    alert("diff is more Tham 10");