文本框中的值不正确

时间:2013-12-11 08:52:22

标签: javascript

我正在尝试一个简单的事情,即从文本框中获取数据,但它没有给我正确的价值?

function cal() {

    var a;
    var b;
    var c;


    //    document.getElementById("cals").innerHTML=btn;
    a = document.getElementById("txta").value;
    b = document.getElementById("txtb").value;
    c = document.getElementById("txtc").value;


    if (a.valueOf() > b.valueOf()) {
        if (a.valueOf() > c.valueOf()) {
            document.write("A is grater");
        } else {
            document.write("c is grater");
        }
    } else {
        if (b.valueOf() > c.valueOf()) {
            document.write("B is grater");
        } else {
            document.write("c is grater");
        }
    }

}

2 个答案:

答案 0 :(得分:0)

尝试使用parseInt,如果是int数字,请尝试使用

            if (parseInt(a, 10)>parseInt(b, 10))
            {    
                if (parseInt(a, 10)>parseInt(c, 10))
                {
                    document.write("A is grater");
                }
                else
                {
                    document.write("c is grater");
                }
            }
            else
            {
                if(parseInt(b, 10)>parseInt(c, 10))
                {
                    document.write("B is grater");
                }else
                {
                    document.write("c is grater");
                }
            }

答案 1 :(得分:0)

function cal() {
    var a,b,c, text;

    a = parseInt(document.getElementById("txta").value,10);
    b = parseInt(document.getElementById("txtb").value,10);
    c = parseInt(document.getElementById("txtc").value,10);

    if (a > b && a > c) text = "A is grater"
    else if (b > c) text = "B is grater"
    else  text = "C is grater"

    document.write(text);
}