a1w2s3d3 2019-04-26 18:48 采纳率: 0%
浏览 534

怎么在输入内容不合法的时候不让注册跳转

1.我输入内容不正确时有提示,但点注册依旧能成功,怎么在输入内容不正确时点注册不能提交?
2.代码如下:

<script>
    $(function verify() {
        $('input').eq(0).focus(
            function() {
                if ($(this).val().length == 0) {
                    $(this).parent().next("div").text(
                            "长度为4-20位");
                    $(this).parent().next("div").css(
                            "color", '#ccc');
                }
            })
        $('input').eq(1).focus(
            function() {
                if ($(this).val().length == 0) {
                    $(this).parent().next("div").text(
                            "长度为6-20位");
                    $(this).parent().next("div").css(
                            "color", '#ccc');
                }
            })
        $('input').eq(0).blur(
            function() {
                if ($(this).val().length == 0) {
                    $(this).parent().next("div").text("");
                    $(this).parent().next("div").css(
                            "color", 'red');
                } else if ($(this).val().length > 10
                        || $(this).val().length < 4) {
                    $(this).parent().next("div").text(
                            "长度只能在4-20位");
                    $(this).parent().next("div").css(
                            "color", 'red');
                } else {

                    $(this).parent().next("div").text("");
                }
            })
        $('input').eq(1).blur(
            function() {
                if ($(this).val().length == 0) {
                    $(this).parent().next("div").text("");
                    $(this).parent().next("div").css(
                            "color", '#ccc');
                } else if ($(this).val().length > 10
                        || $(this).val().length < 6) {
                    $(this).parent().next("div").text(
                            "长度只能在6-20位");
                    $(this).parent().next("div").css(
                            "color", 'red');
                } else {
                    $(this).parent().next("div").text("");
                }
            })
    })
    function check(e) {
        for (var j = 0; j < 2; j++) {
            if ($('input').eq(j).val().length == 0) {
                $('input').eq(j).focus();
                if (j == 1) {
                    $('input').eq(j).parent().next()
                            .next("div").text("此处不能为空");
                    $('input').eq(j).parent().next()
                            .next("div").css("color", 'red');
                    e.preventDefault();
                    return;
                }
                $('input').eq(j).parent().next(".tips").text(
                        "此处不能为空");
                $('input').eq(j).parent().next(".tips").css(
                        "color", 'red');
                e.preventDefault();
                return;
            }
        }
        document.formAdd.submit();
    }
</script>
//注册按钮
<button type="button" class="btn  btn-primary" onclick="check()" id="submit_btn">注册</button>
  • 写回答

1条回答 默认 最新

  • 天字第零号 2019-04-26 19:53
    关注

    不知道为什么你加了return还是会执行 document.formAdd.submit();

    不过建议你吧button 换成input type="submit",这样 e.preventDefault();可以阻止表单提交

    把事件里面加上event参数不然你函数里面的e是undefined

    type="button"是不会阻止表单提交的,因为他只是一个普通按钮js引擎执行的时候也不知道该阻止什么

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog