jQuery实现列表的增加和删除功能
程序员文章站
2022-05-20 22:49:39
具体代码如下所示:
具体代码如下所示:
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jquery</title> <style> </style> </head> <body> <div> <table style="margin: 10px auto;" id="tablelist"> <thead> <tr> <th>name</th> <th>price</th> <th>delete</th> </tr> </thead> <tbody> <tr> <td>a</td> <td>123</td> <td><a href="#" rel="external nofollow" rel="external nofollow" >delete</a></td> </tr> <tr> <td>c</td> <td>3453</td> <td><a href="#" rel="external nofollow" rel="external nofollow" >delete</a></td> </tr> </tbody> </table> </div> <form> <table style="margin:0 auto;"> <tr> <td>name</td> <td><input type="text" name="name"></td> </tr> <tr> <td>price</td> <td><input type="text" name="price"></td> </tr> <tr> <td colspan="2"> <input type="submit" value="submit" id="add"> </td> </tr> </table> </form> <script src="../js/vendor/jquery-3.2.1.min.js"></script> <script> var $tablelist=$("#tablelist"); //tianjia $("#add").click(function(){ var tdname= $("input[name=name]").val(); var tdprice = $("input[name=price]").val(); $("<tr></tr>").append("<td>"+tdname+"</td>") .append("<td>"+tdprice+"</td>") .append("<td><a href='#?'>delete</a></td>") .appendto($("#tablelist>tbody")) .find("a").click(function(){ $(this).parent().parent().remove(); }); $("input[name=name]").val(""); $("input[name=price]").val(""); return false; }) //delete $("#tablelist>tbody a").click(function(){ $(this).parent().parent().remove(); }) </script> </body> </html>
总结
以上所述是小编给大家介绍的jquery实现列表的增加和删除功能,希望对大家有所帮助