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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM