preventDefault不起作用

时间:2015-07-01 20:30:04

标签: javascript jquery forms preventdefault

我遇到了preventDefault函数的问题。它不起作用。这是我的JS文件:

(function() {
    var app = {
        initialize : function () {
            this.setUpListeners();
        },
        setUpListeners: function () {
            $('form').on('submit', app.submitForm);
        },
        submitForm: function(e){
            e.preventDefault();                
        }
    }

    app.initialize(); 
}());

我的HTML代码有2个确认按钮。第二步是隐藏的,应该在第一步完成时显示。但我至少不能在第一步添加preventDefault。

这是我的HTML:

<form id="login">
    <!-- #first_step -->
    <div id="first_step">

        <h1>Registration Form</h1>
        <fieldset id="inputs">
            <div class="form-group">
                <label for="username">Username</label>
                <input type="text" name="username" id="username" class="form-control" placeholder="Create a username" required="">
            </div>

            <div class="form-group">
                <label for="password">Password</label>
                <input type="password" name="password" id="password" class="form-control" placeholder="Create a password" required="">
            </div>

            <div class="form-group">
                <label for="cpassword">Password</label>
                <input type="password" name="password" id="cpassword" class="form-control" placeholder="Confirm your password">
            </div>
        </fieldset>

        <fieldset id="actions">
            <div class="form-group">
                <input type="submit" class="submit" name="submit_first" id="submit_first" value="Next">
            </div>
        </fieldset>
    </div>

    <!-- #second_step -->
    <div id="second_step">
        <h1>Registration Form</h1>
        <fieldset id="inputs">
            <label>Name</label>
            <input type="text" name="name" id="firstname" class="form-control" placeholder="Enter your name" autofocus="" required="">

            <label>Surname</label>
            <input type="text" name="surname" id="lastname" class="form-control" placeholder="Enter your last name" required="">

            <label>Email</label>
            <input type="email" name="email" id="email" class="form-control" placeholder="Enter your email" required="">
        </fieldset>

        <fieldset id="actions">
            <input  class="submit" type="submit" name="submit_second" id="submit_second" value="Next">
        </fieldset>
    </div>
</form>

对不起我只是js的初学者,如果有人帮助我,我将非常感激

1 个答案:

答案 0 :(得分:1)

$(function() {
    var app = {
        initialize : function () {
            this.setUpListeners();
        },
        setUpListeners: function () {
            $('form').on('submit', app.submitForm);
        },
        submitForm: function(e){
            e.preventDefault();                
        }
    }

    app.initialize(); 
});