账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    ajax从后台获取到null中止提交表单
    24
    0
    function check() {//js表单验证方法
            var text = document.getElementById("xh").value;//通过id获取需要验证的表单元素的值
            if (text == "") {
                alert("请输入学号!");//弹出提示
                return false;//返回false(不提交表单)
            }
            if (!(/(^[1-9]\d*$)/.test(text))) {
                alert("请输入学号!");//弹出提示
                return false;//返回false(不提交表单)
            }
            var student_id = $('input[name="student_id"]').val();
            $.post("<%=basePath%>user/queryByUser", 
                { student_id : student_id }).done(function(data) {
                console.log(data.result);
                if (data == null || data == "") {
                    console.log("没有");
                    alert("没有");
                    return false;
                } else {
                    console.log("有");
                }
            }).fail(function() {
                console.error("错误");
            });
            return true;//提交表单
        }
    </script>
    <body>
        <div class="listDIV">
            <table id="FileTable" border="1" width="50%" height="50%"
                style="text-align: center;">
                <form action="<%=basePath%>user/one" method="post">
                    <input type="text" id="xh" name="student_id" placeholder="根据学号查询">
                    <button class="glyphicon glyphicon-select" onclick="return check();">查询</button>
                </form>

    判断为空的时候中止提交表单

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 隔壁小姨子 普通会员 1楼

      在Ajax中,如果从后台获取的数据是null,可以尝试以下几种方法来处理:

      1. 在获取数据前,先检查一下数据是否为null。如果为空,可以返回一个默认值或者提示用户。

      javascript function getData() { // 异步获取数据 $.ajax({ url: 'your-url-here', type: 'GET', success: function(data) { if (data == null) { return 'Data is null or empty!'; } // 假设我们已经有了数据 // 在这里处理数据 } }); }

      1. 在获取数据后,直接使用$.ajax的success回调函数,检查数据是否为空。

      javascript $.ajax({ url: 'your-url-here', type: 'GET', success: function(data) { if (data == null) { return 'Data is null or empty!'; } // 假设我们已经有了数据 // 在这里处理数据 } });

      1. 在成功回调函数中,使用$.ajax的error回调函数,检查是否发生了错误。

      javascript $.ajax({ url: 'your-url-here', type: 'GET', success: function(data) { if (data == null) { return 'Data is null or empty!'; } // 假设我们已经有了数据 // 在这里处理数据 }, error: function() { // 处理错误 } });

      以上都是一些基本的处理方法,具体的处理方式还需要根据你的需求来确定。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部