单击按钮后无法将javascript重定向到另一个html页面

时间:2013-11-13 00:44:19

标签: javascript jquery html

如果变量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?

3 个答案:

答案 0 :(得分:1)

试试这个。

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

window.location.href = "Index.html"

另请查看this Stack Answer

答案 1 :(得分:1)

使用submit事件而不是按钮的click事件:

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/

答案 2 :(得分: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"); 
    }

}