Ajax obtains the form from the background to null to abort the submission of the form.

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>

abort the submission of the form when it is judged to be empty

Aug.29,2021

. The judgment on your side is written as if (! data) {.} else {.}


directly write if (! data), if you want to write like this, use if (data = null, etc.)


  1. do not go to the successful callback branch, you should immediately check whether the HTTP return code is 200. if not, you certainly did not leave
  2. .
  3. or you can add a failure callback. If you don't succeed, you will fail. Anyway, you have to go to
  4. . The
  5. code can be streamlined, and some default parameters don't have to be written out. In addition, it is not recommended to use alert, just Console
< hr >
  

write if (! data) {} judgment directly

Menu