1

如果变量money等于100,我试图让我的javascript代码重定向到Index.html页面。这是html表单代码:

<form id = "form"  action = "">
    <h1> Enter the amount of money you would like to spend on your next trip. </h1><br>

            <div class="input-group">
              <span class="input-group-addon">$</span>
              <input type="text" name="money" class="form-control">

            </div>
            <br>
            <button type="submit" class="btn btn-default" onclick = "MoneyForTrip()">Show Trips</button>

            </form>

和javascript:

var money;

function MoneyForTrip()
{
money = parseInt(
        document.getElementById('form').money.value);

    if (money == 100)
    {
        window.location="Index.html";
    }


    else
    {
        window.alert("Enter another number"); 
    }

}

如果钱等于 100,我知道如何让它重定向到 Index.html 吗?

4

3 回答 3

1

尝试这个。

window.location.replace("Index.html")

或者

window.location.href = "Index.html"

另请查看此 Stack Answer

于 2013-11-13T00:51:24.787 回答
1

使用提交事件而不是按钮的点击事件:

var money;
var form = document.getElementById('form');
form.onsubmit = function(e) {
    e.preventDefault();
    money = parseInt(document.getElementById('form').money.value);

    if (money == 100){
        window.location="Index.html";
    }

    else{
        window.alert("Enter another number"); 
    }
}

演示:http: //jsfiddle.net/xzjW3/

于 2013-11-13T01:00:59.813 回答
0

尝试
<input type="text" name="money" id="moneyfortrip" class="form-control">

var money;

function MoneyForTrip()
{
money = parseInt(
        document.getElementById('moneyfortrip').value);

    if (money == 100)
    {
        window.location="Index.html";
    }


    else
    {
        window.alert("Enter another number"); 
    }

}
于 2013-11-13T00:52:23.487 回答