2015-07-01 35 views
0

我有一个preventDefault函数的问题。它不起作用。这里是我的JS文件:preventDefault不起作用

(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会很感激,如果有人帮我

+0

99%确定您在'(函数()...') – Amit

+0

@Amit是匿名函数之前忘了'$' –

+0

@Amit为什么应该有'$'?这是一个自我调用的匿名函数,语法很好。 ** @ OP **:你是否得到任何JS错误? – Quasdunk

回答

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

    app.initialize(); 
}); 
相关问题