Home >Web Front-end >JS Tutorial >jQuery implements the adding and deleting functions of user information table
This article mainly introduces the jQuery implementation of the user information table addition and deletion functions. The code is simple and easy to understand, very good, with reference and jquery The value, friends who are interested in jquery can refer to this article
1. Browser interface
A simple User information operation
2, html code
<body> <form name="userForm"> <center> 用户录入 <br /> 用户名: <input id="username" name="username" type="text" size=15 /> E-mail: <input id="email" name="email" type="text" size=15 /> 电话: <input id="tel" name="tel" type="text" size=15 /> <input type="button" value="添加" id="btn_submit" /> <input type="button" value="删除所有" id="btn_removeAll" /> </center> </form> ---------------------------- <hr /> <table border="1" align="center" cellpadding=0 cellspacing=0 width=400> <thead> <tr> <th>用户名</th> <th>E-mail</th> <th>电话</th> <th>操作</th> </tr> </thead> ---------------------------- <tbody id="userTbody"> <tr> <td>乔峰</td> <td>qiao@163.com</td> <td>18212345678</td> <td> <a href='#' class='myClz'>删除</a> </td> </tr> </tbody> ---------------------------- </table> </body>
3, jQuery implementation:
$(function () { $("#btn_submit").click(function () { // 获取用户输入的值 var usernameVal = $("#username").val(); var emailVal = $("#email").val(); var telVal = $("#tel").val(); var tr = "<tr><td>" + usernameVal + "</td><td>" + emailVal + "</td><td>" + telVal + "</td><td><a href='#' class='myClz'>删除</a></td></tr>"; $("#userTbody").append(tr); }); // 全部删除 $("#btn_removeAll").click(function () { $("#userTbody").empty(); }); //删除一行数据 /*click只对本身页面有的元素有作用,对于后面新加的元素,不起作用 $(".myClz").click(function() { console.log(123); }); */ /*选择id=userTbody元素下所有样式名含有myClz的标签,并添加click事件 *当点击后,向上一级找到tr元素,然后删除 */ $('#userTbody').on('click', ".myClz", function () { $(this).closest('tr').remove(); }); });
The above is the editor I would like to introduce to you jQuery to implement the adding and deleting functions of user information tables. I hope it will be helpful to you! !
Related recommendations:
Detailed explanation of JavaScript module pattern
Take you to quickly understand the event model in JavaScript
Detailed explanation of JavaScript observer pattern examples
The above is the detailed content of jQuery implements the adding and deleting functions of user information table. For more information, please follow other related articles on the PHP Chinese website!