日期:2014-05-16  浏览次数:20836 次

ajax删除时总是删除最后一个记录,什么问题 - Web 开发 / Ajax
页面代码如下

HTML code
<%@ 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 'MyAjax.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">
    var request;
    
    function createRequest()
    {
        if(window.ActiveXObject)
        {    
            try{
                request=new ActiveXObject("Msxml2.XMLHTTP");
            }catch(e){
                request=new ActiveXObject("Microsoft.XMLHTTP");
            }
        }else if(window.XMLHttpRequest){
            request=new XMLHttpRequest();
        }
    }
    
    function sendRequest()
    {
        createRequest();
        var url="servlet/ajaxTest1";
        request.open("GET",url,true);
        
        request.onreadystatechange=processResponse;
        
        request.send(null);
    }
    
    function processResponse()
    {
        
        if(request.readyState==4)
        {
            if(request.status==200)
            {
            
                Display();
            }
        }else 
            {
                
            }
    }
    
    function Display()
    {
    
        var num=request.responseXML.getElementsByTagName("num")[0].firstChild.nodeValue;
        
        for(var i=0;i<num;i++)
        {
            var id=request.responseXML.getElementsByTagName("id")[i].firstChild.nodeValue;
            
            var name=request.responseXML.getElementsByTagName("name")[i].firstChild.nodeValue;
            var tbody=document.getElementById("content");
            var tr=document.createElement("tr");
            tr.setAttribute("id",id);
            var td1=document.createElement("td");
            td1.appendChild(document.createTextNode(id));
            tr.appendChild(td1);
            var td2=document.createElement("td");
            td2.appendChild(document.createTextNode(name));
            tr.appendChild(td2);
            var delbtn=document.createElement("input");
            delbtn.setAttribute("type","button");
            delbtn.setAttribute("value","delete");
            delbtn.setAttribute("id","btn"+id);
            
            delbtn.onclick=function(){delContent(id);};
            var td3=document.createElement("td");
            td3.appendChild(delbtn);
            tr.appendChild(td3);
            document.getElementById("content").appendChild(tr);
            

        }

    }
    
    function delContent(id)
    {
        
        createRequest();
        
        var url="servlet/ajaxTest1?action=delete&id="+id;
        request.onreadystatechange=deleteChange;
        r