当用户单击与该行对应的编辑按钮时,如何将显示的一行数据库转换为文本框

时间:2014-08-19 10:20:49

标签: java javascript ajax jsp

我是java.i的新手我正在通过jsp显示我的数据库内容。我希望当用户单击与正在显示的该行数据库对应的编辑按钮时,该行内容应显示在文本框中,而其余行保持不变。

我的jsp代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>

<script language="javascript">


function deleteData(id){
    var xmlhttp;
    if(window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
}
else{
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.open("GET","http://localhost:8080/First/deleteData1?id="+id+"&format=fragment",true);
xmlhttp.send(null);

xmlhttp.onreadystatechange = function()
{
    //alert(xmlhttp.status==200);
    if(xmlhttp.readyState==4 && xmlhttp.status==200){
        //alert(xmlhttp.responseText);
    document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
    }
}


}

function editData(id){

    var xmlhttp1;
    if(window.XMLHttpRequest){
    xmlhttp1 = new XMLHttpRequest();
}
else{
    xmlhttp1 = new ActiveXobject("Microsoft.XMLHTTP");
}

xmlhttp1.open("GET","",true);
xmlhttp1.send(null);

xmlhttp.onreadystatechange = function()
{
    if(xmlhttp.readyState==4 && xmlhttp.status==200){
        document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
    }
}

}

</script>

</head>
<body>
<div id="myDiv">
    <form>
    <table border='1' width='300' cellpadding='1' cellspacing='0'>
        <tr>
            <td colspan=7 align="center"></td>
        </tr>
        <tr>
            <td>Id</td>
            <td>Name</td>
            <td>Department</td>
            <td>Password</td>
            <td>Edit</td>
            <td>Delete</td>
        </tr>

        <%
            List Li = new ArrayList();
            Iterator lr;

            if (request.getAttribute("userList") != null
                    && request.getAttribute("userList") != "") {
                List userList = (ArrayList) request.getAttribute("userList");
                Iterator itr = userList.iterator();

                while (itr.hasNext()) {
                    Li = (ArrayList) itr.next();
                    lr = Li.iterator();
                    Integer id = (Integer) lr.next();
                    String name = (String) lr.next();
                    String department = (String) lr.next();
                    String password = (String) lr.next();
        %>

        <tr>
            <td><%=id%></td>
            <td><%=name%></td>
            <td><%=department%></td>
            <td><%=password%></td>

            <td><input type="button" name="edit" value="edit" onclick="editData(<%=id%>);"></td>
            <td><input type="button" name="delete" value="Delete" onclick="deleteData(<%=id%>);"></td>
        </tr>

        <%
            }
            }
        %>
    </table>
    </form>
</div>
</body>
</html>

0 个答案:

没有答案