preventDefault不起作用 [英] preventDefault doesn't work

查看:157
本文介绍了preventDefault不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

pre $ $ $ $ $
$ b $ 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>登记表< / h1>
< fieldset id =inputs>
< div class =form-group>
< label for =用户名>用户名< / label>
< input type =textname =usernameid =usernameclass =form-controlplaceholder =Create a usernamerequired =>
< / div>

< div class =form-group>
< label for =password>密码< / label>
< input type =passwordname =passwordid =passwordclass =form-controlplaceholder =Create a passwordrequired =>
< / div>

< div class =form-group>
< label for =cpassword>密码< / label>
< input type =passwordname =passwordid =cpasswordclass =form-controlplaceholder =Confirm your password>
< / div>
< / fieldset>

< fieldset id =actions>
< div class =form-group>
< input type =submitclass =submitname =submit_firstid =submit_firstvalue =Next>
< / div>
< / fieldset>
< / div>

<! - #second_step - >
< div id =second_step>
< h1>登记表< / h1>
< fieldset id =inputs>
< label>名称< / label>
< input type =textname =nameid =firstnameclass =form-controlplaceholder =输入您的姓名autofocus =required =>

< label>姓< / label>
< input type =textname =surnameid =lastnameclass =form-controlplaceholder =输入您的姓氏required =>

< label>电子邮件< / label>
< input type =emailname =emailid =emailclass =form-controlplaceholder =输入您的电子邮件地址required =>
< / fieldset>

< fieldset id =actions>
< input class =submittype =submitname =submit_secondid =submit_secondvalue =Next>
< / fieldset>
< / div>
< / form>

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

$()函数(){
var app = {
初始化:函数(){
this.setUpListeners();
},
setUpListeners:function() ('submit',app.submitForm);
},
submitForm:function(e){
e.preventDefault();
}
}

app.initialize();
});


I have a problem with preventDefault function. It doesn't work. Here is my JS file:

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

    app.initialize(); 
}());

My HTML code has 2 confirm button. Second step is hidden and should be display, when first step will be done. But I can't at least add preventDefault for first step.

Here is my 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>

I'm sorry. I'm just a beginner in js and will be very thankful if someone help me

解决方案

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

    app.initialize(); 
});

这篇关于preventDefault不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆