javascript 获取if/else语句的元素ID

piv4azn7  于 2023-03-16  发布在  Java
关注(0)|答案(7)|浏览(140)

正在做一个项目。我需要else语句来工作,但是当我点击提交按钮时,它只是重定向到“if”

<head>
<script>
    var judgmentAmount = document.getElementById('judgmentAmount');
    var amountCollected = document.getElementById('amountCollected');
    function judgmentCalc() {
        if( judgmentAmount - amountCollected < 10000 ) {
            window.open( 'http://afjudgment.com' );
        }else {
            window.open( 'http://www.yahoo.com' );
        }
    }
</script>
</head>

<body>
<h1>Application</h1>
<p>I understand that this application neither obligates me to <b>Affirmative     Judgment</b> nor does it obligate <b>Affirmative Judgment</b> to me.</p>
<h3>Judgment Information</h3>
<form>
    <span>Do you have a case number?</span>
    <input type="text" name="caseNumber" />
    <span>Amount of Judgment</span>
    <input type="text" name="judgmentAmount" id="judgmentAmount"  />
    <span>Amount collected to date</span>
    <input type="text" name="amountCollected" id="amountCollected"  />
    <input type="submit" name="submitButton" onclick="judgmentCalc();" />
</form>
bkhjykvo

bkhjykvo1#

您需要访问这些输入字段的值,而不是直接处理document.getElementById()返回的dom元素。您可以使用.value属性获取input的值。

var judgmentAmount = document.getElementById('judgmentAmount').value;
var amountCollected = document.getElementById('amountCollected').value;
apeeds0o

apeeds0o2#

将变量移到函数内部,以便在函数执行时正确填充,并添加DOM元素的.value属性。
试试这个:

function judgmentCalc() {
      var judgmentAmount = document.getElementById('judgmentAmount').value;
      var amountCollected = document.getElementById('amountCollected').value;
        if( judgmentAmount - amountCollected < 10000 ) {
            window.open( 'http://afjudgment.com' );
        }else {
            window.open( 'http://www.yahoo.com' );
        }
    }
xcitsw88

xcitsw883#

您只是比较对象,需要比较它们的值

var judgmentAmount = Number(document.getElementById('judgmentAmount').value);
var amountCollected = Number(document.getElementById('amountCollected').value);

此外,在比较之前将它们转换为“数字”。

function judgmentCalc() {
    var judgmentAmount = Number(document.getElementById('judgmentAmount').value);
    var amountCollected = Number(document.getElementById('amountCollected').value);
    if( judgmentAmount - amountCollected < 10000 ) {
        window.open( 'http://afjudgment.com' );
    }else {
        window.open( 'http://www.yahoo.com' );
    }
}
tkclm6bt

tkclm6bt4#

您将在此处获得HTML DOM元素:

var judgmentAmount = document.getElementById('judgmentAmount');
var amountCollected = document.getElementById('amountCollected');

因此在judgementCalc方法中,需要获取这些元素的值:

function judgmentCalc() {
    // notice that we're using VALUE here:
    if( judgmentAmount.value - amountCollected.value < 10000 ) {
        window.open( 'http://afjudgment.com' );
    }else {
        window.open( 'http://www.yahoo.com' );
    }
}
hiz5n14c

hiz5n14c5#

您需要使用元素的值进行操作:

function judgmentCalc() {

  var judgmentAmount = +document.getElementById('judgmentAmount').value;
  var amountCollected = +document.getElementById('amountCollected').value;

  if( judgmentAmount - amountCollected < 10000 ) {
    window.open( 'http://afjudgment.com' );
  } 
  else {
    window.open( 'http://www.yahoo.com' );
  }
}

var judgmentAmount = document.getElementById('judgmentAmount')只返回一个ID为“judgmentAmount”的元素的引用。如果你想使用元素的值,你需要这样做:
var judgmentAmount = document.getElementById('judgmentAmount').value .
下一步是计算某个数量。您需要将string转换为int(使用+运算符)

typeof judgmentAmount; // "string"

typeof +judgmentAmount; // "number"

Demo

up9lanfz

up9lanfz6#

请运行下面的脚本,它将正常工作:

<head>
<script>
    function judgmentCalc() {
        var judgmentAmount = document.getElementById('judgmentAmount').value;
        var amountCollected = document.getElementById('amountCollected').value;

        var exp = parseFloat(judgmentAmount) - parseFloat(amountCollected);

        if( exp < 10000 ) {
            window.open( 'http://afjudgment.com' );
        } else {
            window.open( 'http://www.yahoo.com' );
        }
    }
</script>
</head>
aamkag61

aamkag617#

必须比较输入元素的值,请尝试以下操作

function judgmentCalc() {
    var judgmentAmount = document.getElementById('judgmentAmount').value;
    var amountCollected = document.getElementById('amountCollected').value;

    if( (judgmentAmount - amountCollected) < 10000 ) {
        window.open( 'http://afjudgment.com' );
    } else {
        window.open( 'http://www.yahoo.com' );
    }
}

也许能帮到你。

相关问题