温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

jquery 版本的动态编辑表格 三

发布时间:2020-07-15 08:31:52 来源:网络 阅读:238 作者:huangyanxiong 栏目:web开发
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>练习</title>
</head>
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<style type="text/css">
    .user{
        color: #DFDFDF;
    }
    table {
    border: 1px solid black;
    /*修正单元格之间的边框不能合并*/
    border-collapse: collapse;
    width:600px;
    }
    table td {
        border: 1px solid black;
        width: 25%;
    }
    table th {
        border: 1px solid black;
        width: 25%;
    }
    tbody th {
        background-color: #A3BAE9;
    }
</style>
<body>
    <div>
    <form name="form1" id="form1" >
        username:<input type="text" id="username" name='user'  />
        mail:<input type="text" id="mail" name="user" />
        phone:<input type="text" id="phone" name="user" />
        <input type="button" value="添加" id="add" />
    </form>
    </div>
    <div >
        <table  id="table">
            <tr>
                <th align="center" >username</th>
                <th align="center" >mail</th>
                <th align="center" >phone</th>
                <th align="center" >操作</th>
            </tr>
            <tr>
                <td align="center">huangyanxiong</td>
                <td align="center">huang@qq.com</td>
                <td align="center">12345678912</td>
                <td align="center">
                <input type="button" value="编辑" name="editInput">
                <input type="button" value="删除" name="delInput">
                </td>
            </tr>
        </table>
    </div>
</body>
<script type="text/javascript">
    $(document).ready(function(){
        /*
            1版后话:这样会存在一个问题就是表格中已经存在一些原有的表格行数据并且没有按过添加按钮时时就不能进行编辑和删除,需要对代码进行调整
            2版问题:这样会造成点击一次编辑或者删除按钮会变成两次按钮,而且代码不能复用
            3版(this):利用函数解决代码复用,但是jquery不支持这样做
        */
        function  adduser(){
            var td=new Array();
            var editInput="<input type='button' value='编辑' name='editInput'/>";
            var delInput="<input type='button' value='删除' name='delInput'/>";
            var i=0;
                  
            $("input[name='user']").each(function(){
                td[i]="<td align='center'>"+$(this).val()+"</td>";  //专门获取属性值
                i++;
            });
            $('#table').append('<tr>'+td.join('')+"<td align='center'>"+editInput+' '+delInput+"</td>"+'</tr>');
        }
        function delInput(){
            $("input[name='delInput']").click(function(){
                if (confirm('确定要删除吗')) {
                    $(this).parent().parent().remove();  //jquery最大特点可以自己删除自己
                };
            });
        } 
        function editInput(){
            $("input[name='editInput']").click(function(){
                var i=1;
                if ($(this).val()=='编辑') {
                $(this).parent().siblings().each(function(){
                    $(this).html('<input type="text" value='+'"'+$(this).text()+'"'+''+' />');
                        i++;
                    });
                    $(this).val('保存');
                    return false;
                };
                if ($(this).val()=='保存') {
                    $(this).parent().siblings().each(function(){
                        $(this).text($(this).children().val());
                    i++;
                    });
                    $(this).val('编辑');
                    return false;
                };
            });
        }
        $('#add').click(function(){
            adduser();
            editInput();
            delInput();
        }
        editInput();
        delInput();
    });
</script>
</html>


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI