js表格检查和提交?

时间:2015-06-06 02:48:30

标签: javascript jquery html forms

修订问题: 按下我当前表单中的订阅按钮,按钮标签变为"发送"但是没有过去。有适当的代码应该更改按钮的标签以显示"消息已发送。"邮件,或者在这种情况下,电子邮件和名称表单提交到我的mailchimp帐户,实际上确实已发送,但用户根据按钮状态不知道它。这是我的问题。

这是我的HTML:

<form action="http://sawtoothgroup.us5.list-manage.com/subscribe/post"
    class="col-xs-12 col-sm-12 col-md-6" id="contact-form" method="post" name=
    "contact-form" novalidate="">
        <input name="u" type="hidden" value="34f19b17d46b112e06d8ecccd">
        <input name="id" type="hidden" value="6527c49f30">

        <div class="form-group">
            <label class="control-label" for="MERGE0">Email</label>

            <div class="controls">
                <input class="form-control input-lg requiredField"
                data-error-empty="Please enter your email" data-error-invalid=
                "Invalid email address" id="MERGE0" name="MERGE0" placeholder=
                "Your email" type="email">
            </div>
        </div>

        <div class="form-group">
            <label class="control-label" for="MERGE1">First Name</label>

            <div class="controls">
                <input class="form-control input-lg requiredField"
                data-error-empty="Please enter your first name" id="MERGE1"
                name="MERGE1" placeholder="First name" type="text">
            </div>
        </div>

        <div class="form-group">
            <label class="control-label" for="MERGE2">Last Name</label>

            <div class="controls">
                <input class="form-control input-lg requiredField"
                data-error-empty="Please enter your last name" id="MERGE2"
                name="MERGE2" placeholder="Last name" type="text">
            </div>
        </div>

        <p><button class="btn btn-king btn-block" data-error-message="Error!"
        data-ok-message="Message Sent" data-sending-message="Sending..." name=
        "submit" type="submit"><i class=
        "icon-location-arrow"></i>Subscribe</button></p><input id="submitted"
        name="submitted" type="hidden" value="true">
</form>

这是我的js:

$(document).ready(function() {
    $('#contact-form').submit(function() {
        var buttonCopy = $('#contact-form button').html(),
            errorMessage = $('#contact-form button').data('error-message'),
            sendingMessage = $('#contact-form button').data('sending-message'),
            okMessage = $('#contact-form button').data('ok-message'),
            hasError = false;
        $('#contact-form .error-message').remove();
        $('#contact-form .requiredField').each(function() {
            $(this).removeClass('inputError');
            if($.trim($(this).val()) == '') {
                var errorText = $(this).data('error-empty');
                $(this).parents('.controls').append('<span class="error-message" style="display:none;">'+errorText+'.</span>').find('.error-message').fadeIn('fast');
                $(this).addClass('inputError');
                hasError = true;
            } else if($(this).is("input[type='email']") || $(this).attr('name')==='MERGE0') {
                var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                if(!emailReg.test($.trim($(this).val()))) {
                    var invalidEmail = $(this).data('error-invalid');
                    $(this).parents('.controls').append('<span class="error-message" style="display:none;">'+invalidEmail+'.</span>').find('.error-message').fadeIn('fast');
                    $(this).addClass('inputError');
                    hasError = true;
                }
            }
        });
        if(hasError) {
            $('#contact-form button').html('<i class="fa fa-times"></i>'+errorMessage).addClass('btn-error');

            setTimeout(function(){
                $('#contact-form button').removeClass('btn-error').html(buttonCopy);

            },2000);
        }
        else {
            $('#contact-form button').html('<i class="icon-spinner icon-spin"></i>'+sendingMessage);

            var formInput = $(this).serialize();
            $.post($(this).attr('action'),formInput, function(data){
                $('#contact-form button').html('<i class="fa fa-check"></i>'+okMessage);
                setTimeout(function(){
                    $('#contact-form button').html(buttonCopy);
                },2000);

            });
        }
        return false;   
    });
});

这是我的css:

.contact-item h2{
    font-size:18px;
    font-weight:600;
    margin-bottom:10px;
}
.contact-item .icon{
    display:block;
    font-size:48px;
    color:#5cc9df;
    text-shadow:-2px 2px 0 rgba(0,0,0,0.1);
    -webkit-transition:all .3s ease-out;
    transition:all .3s ease-out;
}
.contact-item .icon:hover{
    color:#5cc9df;
    -webkit-transform:scale(1.3) translateY(-10px);
    transform:scale(1.3) translateY(-10px);
}
.bl_form {
    margin: 30px 0 0;
}
.bl_form input {
    padding-top: 15px;
    background: rgba(255,255,255,0.10);
    box-shadow: 0 4px 0px rgba(0,0,0,0.2);
    border: none;
    color: white;
    padding: 10px 15px;
    border-radius: 5px;
    font-size: 16px;
    outline: none;
}
.lb_wrap .lb_label.top, 
.lb_wrap .lb_label.bottom {
    left: 66px !important;
}
.lb_wrap .lb_label.left {
    left: 0;
}
.lb_label {
    font-size:17px;
    line-height:32px;
    font-weight: 400;
    color: #ccc;
}
.no-placeholder .lb_label {
    display:none;
}
.lb_label.active {
    color: #aaa;
}
#contact-form .form-group label{
    display:none;
    font-size:18px;
    line-height:24px;
    font-weight:100;
    text-transform:uppercase;
}
#contact-form.no-placeholder .form-group label{
    display:block;
}
#contact-form .controls {
    padding:0;
}
#contact-form.no-placeholder .controls {
    margin-top:0;
}
#contact-form .form-control,
.input-group .form-control {
    display:inline;
    background:#fff;
    border:none;
    border-radius:5px;
    outline:none;
    height:52px;
    font-size:18px;
    line-height:32px;
    color:#aaa;
    font-weight:400;
    vertical-align:top;
    border:2px solid #FFF;
    box-shadow:none;
}
#contact-form .form-group.half-width{
    width:40%;
    float:left;
}
#contact-form .btn-king{
    margin: 0;
}
#contact-form .form-group{
    position:relative;
}
#contact-form .form-group [class*=fa] {
    display:block;
    width:64px;
    position:absolute;
    top:0;
    left:5px;
    color:#eee;
    font-size:24px;
    line-height:52px;
    text-align:center;
    font-weight:300;
    -webkit-transition:color .3s ease-out;
    transition:color .3s ease-out;
}
#contact-form .form-group [class*=fa].active{
    color:#ccc;
}
#contact-form.no-placeholder .form-group [class*=fa]{
    top:30px;
}
#contact-form textarea.form-control {
    height:auto;
    max-width:100%;
    min-width:100%;
    font-size:18px;
    font-weight:400;
    line-height:24px;
    padding-top:14px;
    vertical-align:top;
    border:2px solid #FFF;
}
#contact-form .form-control:focus, 
.input-group .form-control:focus {
    outline:none;
    border:2px solid #2AAFC3;
}
#contact-form .inputError,
#contact-form textarea.form-control.inputError,
#newsletter-form .inputError {
    outline:none;
    border:2px solid #F05557;
}

#contact-form .error-message {
    position:absolute;
    top:15px;
    right:12px;
    font-size:15px;
    line-height:24px;
    font-weight:400;
    color:#F05557;
    z-index:10;
}
#contact-form.no-placeholder .error-message {
    top:0;
}
.input-group-btn .btn-king {
    margin: 0;
}
#main-footer,
#social {
    padding: 40px 0 20px;
}
#topcontrol {
    background: #84aac9;
    color: #fff;
    text-align: center;
    display: inline-block;
    width: 35px;
    height: 35px;
    border: none;
    -webkit-border-radius: 2px;
    -moz-border-radius: 2px;
    -ms-border-radius: 2px;
    -o-border-radius: 2px;
    border-radius: 2px;
    -moz-background-clip: padding;
    -webkit-background-clip: padding-box;
    background-clip: padding-box;
    -webkit-transition: all 0.4s ease-in-out;
    -moz-transition: all 0.4s ease-in-out;
    -ms-transition: all 0.4s ease-in-out;
    -o-transition: all 0.4s ease-in-out;
}
#topcontrol:hover {
    background: #6091ba;
}
#topcontrol .fa {
    position: relative;
    top: 3px;
    font-size: 25px;
}

请让我知道我做错了什么......

http://jsfiddle.net/daCrosby/154codmr/

0 个答案:

没有答案