日期:2014-05-16 浏览次数:20492 次
<tr id="tr1" class="tr">
    <td id="td1"></td>
</tr>
<tr id="tr2" class="tr">
    <td id="td1"></td>
</tr>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jquery 修改表格</title>
<script type="text/javascript" src="jquery-1.6.4.min.js" ></script>
</head>
<body>
<table id="table1">
<tr><th>姓名</th><th>年龄</th><th>操作</th></tr>
<tr><td>ss</td><td>1</td><td>
<input type="button" value="delete" onclick="var  tr=this.parentNode.parentNode;tr.parentNode.removeChild(tr);" />
</td></tr>
</table>
行号<input id="rowindex"    />
姓名<input id="username"  />
年龄<input id="age"  />
<input  type="button" value="保存" id="save"    />
<script type="text/javascript" >
    $("#save").click(function() {
        if ($("#rowindex").val() == "") {//新增行
            $("<tr><td>" + $("#username").val() + "</td><td>" + $("#age").val() + "</td><td><input type='button' value='修改'  /><input type='button' value='删除'  /></td></tr>").appendTo($("#table1")).find("input[type=button]").each(function() {
                $(this).click(function() {
                    if ($(this).val() == "删除") { if (window.confirm("你确定要删除这一行吗")) { $(this).parent().parent().remove(); } }
                    if ($(this).val() == "修改") {
                        var tr = $(this).parent().parent()[0];
                        $("#username").val(tr.cells[0].innerHTML);
                        $("#age").val(tr.cells[1].innerHTML);
                        $("#rowindex").val($(tr).index());
                    }
                }); //click
            }); //apend each
        } //if
        else {//更新行
            var tr = $("#table1 tr").eq($("#rowindex").val())[0];
            tr.cells[0].innerHTML = $("#username").val();
            tr.cells[1].innerHTML = $("#age").val();
        }
    });       //click
    
    
</script>
</body>
</html>
------解决方案--------------------
//举个简单的例子:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-