Javascript 文本框中的值不正确
我正在尝试一件简单的事情,即从文本框中获取数据,但它没有给我正确的值Javascript 文本框中的值不正确,javascript,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 = doc
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");
}
}
}
如果使用parseInt的是int number,请尝试此操作:
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");
}
}
哪里没有得到正确的值?你有a,b,c值吗?是a值吗?或者a/b/c是什么?它在这里绝对有效,不使用没有基数的
parseInt
。是的,我已经纠正了,使用基数是parseInt获得正确值的必要条件,并且没有奇怪的问题,谢谢@quentin为什么不在声明“b”时解析文本框值(b=parseInt(document.getElementById(“txtb”).value,10))而不是多次将“b”解析为int?当然,“a”和“c”也是如此
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);
}