获取if / else语句的元素ID

时间:2016-05-05 05:19:31

标签: javascript forms if-statement getelementbyid window.open

处理项目。我需要使用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>

7 个答案:

答案 0 :(得分:0)

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

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&#34; judgmentAmount&#34;的元素的引用。如果要使用元素的值,则需要执行以下操作:

var judgmentAmount = document.getElementById('judgmentAmount').value

下一步是计算一些金额。您需要将字符串转换为int(使用+运算符)

typeof judgmentAmount; // "string"

typeof +judgmentAmount; // "number"

Demo

答案 1 :(得分:0)

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

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' );
    }
}

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

在函数内移动变量,以便在函数执行时正确填充它们,并添加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' );
        }
    }

答案 4 :(得分:0)

您在这里获取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' );
    }
}

答案 5 :(得分:-1)

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

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

答案 6 :(得分:-1)

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

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' );
    }
}

它可能对你有帮助。