Ajax点击button按钮不进行跳转

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
<script type="text/javascript"
    src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
<script type="text/javascript">
    $(function() {
        var name = false;
        var d_id = false;
        var position = false;
        var salary = false;
        var da = false;
        $("#name").blur(function() {
            $("#ename").empty();
            e_name = $(this).val();
            if ($(this).val() == null || $(this).val() == '') {
                $(this).after($("<span id='ename'>用户名不能为空<span>"));
                name = false;
            } else {
                name = true;
            }
        });

        $("#d_id").blur(function() {
            $("#ed_id").empty();
            e_d_id = $(this).val();
            if ($(this).val() == null || $(this).val() == '') {
                $(this).after($("<span id='ed_id'>部门类型不能为空<span>"));
                d_id = false;
            } else {
                d_id = true;
            }
        });

        $("#position").blur(function() {
            $("#eposition").empty();
            e_position = $(this).val();
            if ($(this).val() == null || $(this).val() == '') {
                $(this).after($("<span id='eposition'>职位不能为空<span>"));
                position = false;
            } else {
                position = true;
            }
        });

        $("#salary").blur(function() {
            $("#esalary").empty();
            e_salary = $(this).val();
            if ($(this).val() == null || $(this).val() == '') {
                $(this).after($("<span id='esalary'>薪资不能为空<span>"));
                salary = false;
            } else {
                salary = true;
            }
        });

        $("#da").blur(function() {
            $("#eda").empty();
            e_da = $(this).val();
            if ($(this).val() == null || $(this).val() == '') {
                $(this).after($("<span id='eda'>日期不能为空<span>"));
                da = false;
            } else {
                da = true;
            }
        });

        $("#tj")
                .click(
                        function() {
                            if (name && d_id && position && salary && da) {
                                $
                                        .post(
                                                "${pageContext.request.contextPath }/emp/insert",
                                                {
                                                    "name" : e_name,
                                                    "d_id" : e_d_id,
                                                    "position" : e_position,
                                                    "salary" : e_salary,
                                                    "da" : e_da
                                                },
                                                function(a) {
                                                    if (a == "ok") {
                                                        alert("添加成功!");
                                                        location.href = "${pageContext.request.contextPath }/emp/selectAll";
                                                    } else {
                                                        alert("添加失败!");
                                                        location.reload();
                                                    }

                                                }, "json");
                            } else {
                                alert("请正确填写信息!");
                            }
                        });
    });
</script>
</head>

<body>
    <form action="${pageContext.request.contextPath }/emp/insert"
        method="post">
        用户名: <input type="text" name="empname" id="name"><br>
        所属部门: <input type="text" name="d_id" id="d_id"><br> 职位: <input
            type="text" name="position" id="position"><br> 薪资: <input
            type="text" name="salary" id="salary"><br> 入职日期: <input
            type="date" name="da" id="da"><br> <input type="button"
            value="注册" id="tj" />

    </form>
</body>
</html>

 

这是正确代码,错误是将    if (name && d_id && position && salary && da) 中的d_id写成了e_id

转载于:https://www.cnblogs.com/Dream--/p/7084331.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值