如何防止页面重新加载

时间:2017-02-08 11:44:06

标签: jquery html5 css3

当我点击登录按钮页面正在重新加载因此它显示我在jquery中写的隐藏内容。所以我写了防止默认功能,即使它不起作用。那么请你帮帮我吗?

      $('.tab a').on('click', function (e) {
                        e.preventDefault();
                        $(this).parent().addClass('active');
                        $(this).parent().siblings().removeClass('active');
                        target = $(this).attr('href');
                        $('.tab-content > div').not(target).hide();
                        $(target).fadeIn(600);
                    });

            
                    $(document).ready(function () {
                        $("#otp").hide();
                        $("#setpswd").hide();
                        $(".cancel").hide();
                        $("#submit").hide();
                        $(".login").show();
                        $(".forgot").click(function (evt) {
                             evt.preventDefault();
                             return false();
                            $("#otp").show();
                            $("#setpswd").show();
                            $("#pswd").hide();
                            $(".cancel").show();
                            $(".forgot").hide();
                            $("#submit").show();
                            $(".login").hide();
                        });
                    });
                   
.form { 
        border-top: 4px solid #0689b2;
        background: #45bacc;
        padding: 30px;
        max-width: 400px;
        margin: 40px auto;
        border-radius: 4px;
        /*box-shadow: 0 4px 10px 4px rgba(19, 35, 47, 0.3);*/
    }
    .box-shadow{
        box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
        padding: 7%; 
        background: rgba(6, 137, 178, 0.68);
    }
    .tab-group {
        list-style: none;
        padding: 0;
        margin: 0 0 40px 0;
    }
    .tab-group:after {
        content: "";
        display: table;
        clear: both;
    }
    .tab-group li a {
        display: block;
        text-decoration: none;
        padding: 5px;
        background: #0689b2;
        color: #ffffff;
        font-size: 20px;
        float: left;
        width: 50%;
        text-align: center;
        cursor: pointer;
        /*  -webkit-transition: .5s ease;
        */transition: .5s ease;
    }
    .tab-content > #signup:last-child {
        display: none;
    }
    .tab-group .active a, .tab-group .active a:hover{
        background: #ffffff;
        color: #0689b2;
    }
    input, textarea {
        display: block;
        width: 100%;
        /*    height: 100%;*/
        padding: 6px 0px 6px 9px;
        background-image: none;
        border-radius: 0;
    }
    input:focus, textarea:focus {
        outline: 0;

    }

    textarea {
        border: 2px solid #a0b3b0;
        resize: vertical;
    }
    ::-webkit-input-placeholder { /* Chrome/Opera/Safari */
        color:#0689b2;
        position:relative;
        left:10px;
    }
    ::-moz-placeholder { /* Firefox 19+ */
        color:#0689b2;
        position:relative;
        left:10px;
    }
    :-ms-input-placeholder { /* IE 10+ */
        color:#0689b2;
        position:relative;
        left:10px;
    }
    :-moz-placeholder { /* Firefox 18- */
        color: #fff;
        position:relative;
        left:10px;
    }
    .field-wrap {
        position: relative;
        margin-bottom: 25px;
    }

    .choose-option h4{
        display:inline;
        color: #fff;
        font-weight:800;
    }
    .choose-option {
        margin:0 0 25px 0;
    }
    .choose-option .radio-inline{
        color: #fff;
    }
    .radio-inline input[type=radio] {
        margin: 4px 0 0 34%;
    }
    .top-row:after {
        content: "";
        display: table;
        clear: both;
    }
    .top-row > div {
        float: left;
        width: 48%;
        margin-right: 4%;
    }
    .top-row > div:last-child {
        margin: 0;
    }

    .login-btn {
        border: 0;
        outline: none;
        border-radius: 0;
        padding: 5px;
        font-size: 2rem;
        text-transform: uppercase;
        letter-spacing: .1em;
        background: #0689b2;
        color: #FFF;
        -webkit-transition: all 0.5s ease;
        transition: all 0.5s ease;
        -webkit-appearance: none;
    }
    .login-btn:hover, .login-btn:focus {
        background: #f5f5f5;
        color: #0689b2;
    }

    .button-block {
        /*display: block;*/
        width: 100%;
    }

    .forgot{
        margin: -15px 0px 5px 0;
        text-align: right;
    }
    .forgot a, .cancel a{
        color:#fff;
    }
    .cancel{
        text-align: center;
        margin: 3% 0 0 0;

    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form">
                <div class="box-shadow">
                    <ul class="tab-group">
                        <li class="tab active"><a href="#login">Log In</a></li>
                        <li class="tab"><a href="#signup">Sign Up</a></li>
                    </ul>
                    <div class="tab-content">
                        <div id="login">   
                            <form action="#" method="post">
                                <div class="field-wrap">
                                    <input type="email" placeholder=" Email Address" />
                                </div>

                                <div class="field-wrap" id="pswd">
                                    <input type="password" placeholder=" Password" />
                                </div>

                                <p class="forgot"><a href="#">Forgot Password?</a></p>
                                <div class="field-wrap" id="otp">
                                    <input type="text" placeholder="Enter OTP" />
                                </div> 
                                <div class="field-wrap" id="setpswd">
                                    <input type="password" placeholder="Set Password" />
                                </div>
                              <input type="submit" class="login-btn button-block" id="submit" value="Submit">
                                <p class="cancel"><a href="<?php echo base_url(); ?>index.php/welcome/login">Cancel</a></p>
                                <input  type="submit" class="login-btn button-block login" value="Login">
                            </form>
                        </div>

                        <div id="signup">   
                            <form action="#" method="post">
                                <div class="top-row">
                                    <div class="field-wrap">
                                        <input type="text" placeholder="First Name"/>
                                    </div>

                                    <div class="field-wrap">
                                        <input type="text" placeholder="Last Name" />
                                    </div>
                                </div>

                                <div class="field-wrap">
                                    <input type="email"placeholder="Email" />
                                </div>

                                <div class="field-wrap">
                                    <input type="text"placeholder="Mobile Number"/>
                                </div>
                                <div class="choose-option">
                                    <h4>I am<i class="fa fa-angle-right"></i></h4>
                                    <label class="radio-inline">
                                        <input type="radio" name="optradio">Buyer
                                    </label>
                                    <label class="radio-inline">
                                        <input type="radio" name="optradio">Supplier
                                    </label>
                                </div>
                                <div class="field-wrap">
                                    <input type="password"placeholder="Password"/>
                                </div>

                                <div class="field-wrap">
                                    <input type="password"placeholder="Re-enter Password"/>
                                </div>

                                <input type="submit" class="login-btn button-block" value="Sign Up">

                            </form>

                        </div>

                    </div>
                </div>
            </div>

4 个答案:

答案 0 :(得分:0)

试试这个。

$('form').submit( function(e) {
  e.preventDefault();
});

请记住将此代码放在ready函数中。

答案 1 :(得分:0)

您可以在JQuery中添加提交事件来处理您的登录信息;

- (void)textFieldDidEndEditing:(UITextField *)textField {

    NSString *oldText = textField.text;

    textField.text = [NSString stringWithFormat:@"%@ %%",oldText];
}

答案 2 :(得分:0)

或许从jQuery代码中删除最后一个花括号?我已经对它进行了测试,只要我删除了最后一个括号,它会过早地终止脚本,它似乎工作得很好。

$('.tab a').on('click', function (e) {
                    e.preventDefault();
                    $(this).parent().addClass('active');
                    $(this).parent().siblings().removeClass('active');
                    target = $(this).attr('href');
                    $('.tab-content > div').not(target).hide();
                    $(target).fadeIn(600);
                });

                $(document).ready(function () {
                    $("#otp").hide();
                    $("#setpswd").hide();
                    $(".cancel").hide();
                    $("#submit").hide();
                    $(".login").show();
                    $(".forgot").click(function (evt) {
                         evt.preventDefault();
                         return false();
                        $("#otp").show();
                        $("#setpswd").show();
                        $("#pswd").hide();
                        $(".cancel").show();
                        $(".forgot").hide();
                        $("#submit").show();
                        $(".login").hide();
                    });
                });

答案 3 :(得分:0)

由于您使用的是“锚”标记

 <li class="tab active"><a href="#login">Log In</a></li>
 <li class="tab"><a href="#signup">Sign Up</a></li>

每次点击都会刷新。尝试使用span标记。

您可以在点击锚标记时查看页面的刷新方式。 http://plnkr.co/edit/3fvFeTqVqPCTycquhCTl?p=preview