动态编辑表格的一行

在这里插入代码片<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<table width="70%"  border="1" bordercolor="#000000" align="center" id="table1">
    <thead>
    <tr>
        <td>姓名</td>
        <td>学号</td>
        <td>英语</td>
        <td>政治</td>
        <td>操作</td>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
        <td>5</td>
    </tr>
    </tbody>
</table>
<form action="" method="get" style="margin:3em 15%;">
    姓名
    <input type="text" value="" size="20" name="name1" id="name1"/>
    学号
    <input type="text" value="" size="20" name="name2" id="name2"/>
    英语
    <input type="text" value="" size="20" name="name3" id="name3"/>
    政治
    <input type="text" value="" size="20" name="name4" id="name4"/>
    <input type="button" value="提交" id="tj" onclick="dosubmit()">
</form>
</body>
<script type="text/javascript">

    var name = '';
    var Sno = '';
    var english = 0;
    var politics = 0;
    function dosubmit(){
        var xm=document.getElementById("name1").value;
        var xh=document.getElementById("name2").value;
        var yy=document.getElementById("name3").value;
        var zz=document.getElementById("name4").value;

        row=document.getElementById("table1").insertRow(1);
        if(row!=null){
            cell=row.insertCell();
            cell.innerHTML=xm;
            cell=row.insertCell();
            cell.innerHTML=xh;
            cell=row.insertCell();
            cell.innerHTML=yy;
            cell=row.insertCell();
            cell.innerHTML=zz;
            //添加编辑按钮
            cell=row.insertCell();
            cell.innerHTML= '<a href="###" onclick="editRow(this)">编辑</a>';
        }
    }

    //编辑指定行内容
    function editRow(obj) {
        //获取包括要编辑内容的 tr元素
        var tr = obj.parentNode.parentNode;
        name = tr.children[0].innerHTML;
        Sno = tr.children[1].innerHTML;
        english = tr.children[2].innerHTML;
        politics = tr.children[3].innerHTML;
        // console.log(name + ',' + Sno + ',' + english + ',' + politics);
        tr.children[0].innerHTML ='<input type="text" value="' + name + '">';
        tr.children[1].innerHTML ='<input type="text" value="' + Sno + '">';
        tr.children[2].innerHTML ='<input type="text" value="' + english + '">';
        tr.children[3].innerHTML ='<input type="text" value="' + politics + '">';
        tr.children[4].innerHTML ='<a href="###" onclick="saveEdit(this)">确定</a> <a href="###" onclick="cancelEdit(this)">取消</a>';
    }

    //保存编辑内容
    function saveEdit(obj) {
        var tr = obj.parentNode.parentNode;
        tr.children[0].innerHTML = tr.children[0].children[0].value;
        tr.children[1].innerHTML = tr.children[1].children[0].value;
        tr.children[2].innerHTML = tr.children[2].children[0].value;
        tr.children[3].innerHTML = tr.children[3].children[0].value;
        tr.children[4].innerHTML = '<a href="###" onclick="editRow(this)">编辑</a>';
    }

    //取消编辑操作
    function cancelEdit(obj) {
        var tr = obj.parentNode.parentNode;
        tr.children[0].innerHTML = name;
        tr.children[1].innerHTML = Sno;
        tr.children[2].innerHTML = english;
        tr.children[3].innerHTML = politics;
        tr.children[4].innerHTML = '<a href="###" onclick="editRow(this)">编辑</a>';
    }
</script>
</html>

提交数据添加表格一行
点击编辑可以编辑该行的数据进行保存

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值