-1

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

这是我的例子

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" />

首先十分感谢

4

1 回答 1

1

据我了解,您唯一需要做的就是在您计算的地方var diff

if(diff < -10 || diff > 10)
    alert("diff is more Tham 10");
于 2013-01-21T20:46:22.720 回答