判断传回的数据是否为空若为空中止提交表单

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>

return false并没有中止表单提交

阅读 1.9k
3 个回答

ajax设置成同步请求

你这里已经提交过去了。后端接收到了返回data为空。已经生米煮成熟饭了。中止也没用了。

function handleSubmit() {

if (document.form.name.value == "") {
    console.log("请输入内容")
    return false
} 

}

<form name="form" method="post" action="#" onsubmit="return handleSubmit()">

<input name="name" type="text" id="id">
<input type="submit" name="Submit" value="提交">

</form>

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题