在jQuery

时间:2015-08-31 18:48:33

标签: javascript jquery

我正在尝试从数据库中实时检查输入字段。我在下面有 java脚本

$(document).ready(function(){
    $('#emailcheck').load('check-email.php').show();

    $(".next").click(function() {

        $.post('check-email.php', { email: form.email.value },
        function(result) {
            $('#emailcheck').html(result).show();

        });     
    });
});

HTML:

 <form name="email">
    <input type="email" class="form-control" id="email" name="email" placeholder="email"><br>
    <div id="emailcheck"></div>
 </form>
    <p><a class="btn btn-primary next">Go to step 2</a></p> 

正如您所看到的,当我点击.next时,它会告诉我#emailcheck中是否有电子邮件。

如果我从check-email.php获取的结果是“可用”,那很好。 但结果是“采取”,例如,我怎么能阻止.next甚至发生?因为我希望用户在下一步之前修复该字段。

其他Java脚本代码我也用于相同的表单:

        // jQuery.validate script, does client-side validation
        $(document).ready(function(){
            $(".next").click(function(){
                var form = $("#myform");
                form.validate({
                    errorElement: 'div',
                    errorClass: 'formerror',
                    highlight: function(element, errorClass, validClass) {
                        $(element).closest('.form-group').addClass("has-error");
                    },
                    unhighlight: function(element, errorClass, validClass) {
                        $(element).closest('.form-group').removeClass("has-error");
                    },
                    rules: {
                        username: {
                            required: true,
                            minlength: 1,
                            remote: { 
                            url: "check-username.php",
                            async: false,
                            type: "post", }
                        },      
                        email: {
                            required: true,
                            minlength: 1,
                            remote: { 
                            url: "check-email.php",
                            async: false,
                            type: "post", }
                        },

                    },
                    messages: {
                        username: {
                            required: "Username required",
                            remote: "taken",
                        },                  
                        email: {
                            required: "Email required",
                        },                                                      
                    }
                });
                if (form.valid() === true){
                    if ($('#stepusername').is(":visible")){
                        current_fs = $('#stepusername');
                        next_fs = $('#stepemail');
                    }else if($('#stepemail').is(":visible")){
                        current_fs = $('#stepemail');
                        next_fs = $('#stepsuccess');
                    }

                    next_fs.show(); 
                    current_fs.hide();
                }


            });

        });



// prevent sending empty and stuff      
        $(document).ready(function(){
        $('.form-horizontal').keypress(function (event) {
            if (event.which === 13) {
                event.preventDefault();
                $(".next").trigger('click');
            }
        });
         });

// For server-side validation.
        $(document).ready(function(){   
            $(function(){
                $('input[type=submit]').click(function(){
                    if($("#myform").valid()){

                    $.ajax({
                        type: "POST",
                        url: "sent.php",
                        data: $("#myform").serialize(),

                        beforeSend: function(){
                            $('#stepsuccess').html('Loading...');               
                        },
                        success: function(data){
                            $('#stepsuccess').html(data);
                        }

                    });
                    }
                });
            });
             });

1 个答案:

答案 0 :(得分:2)

您实际要做的是阻止“下一次”点击事件的默认行为。因此,您应该将事件声明为$(".next").click(function(event){,然后在if (form.valid())之后添加:

if (form.valid() === true){
   // everything you originally had here
} else {
  event.preventDefault();
}