首页 > Web开发 > 详细

jquery动态添加表单数据

时间:2017-02-19 15:18:19      阅读:144      评论:0      收藏:0      [点我收藏+]
动态添加用户
 
实现代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<!-- 引入jquery开发包 -->
<script type="text/javascript" src="js/jquery-1.12.3.js"></script>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
$(function(){
    // 提交按钮添加 click事件
    $("#addBtn").click(function(){
        
        // 获取form的值
        var name = $("input[name=‘name‘]").val();
        var email = $("input[name=‘email‘]").val();
        var phone = $("input[name=‘phone‘]").val();
        
        // 在table 中生成tr 
        var tr = $("<tr><td>"+name+"</td><td>"+email+"</td><td>"+phone+"</td><td><a href=‘#‘ onclick=‘deleteItem(this);‘>删除</a></td></tr>");
        $("table").append(tr);
        
        // form重置,清除刚才表单填写的内容
        $("form")[0].reset();
    });
});

// 删除
function deleteItem(a){
    // 删除 a 元素所在行 
    $(a).parents("tr").remove();
}
</script>
</head>

<body>
    <div align="center">
        <h3>添加用户</h3>
        <form>
            姓名 <input type="text" name="name" />
            邮箱 <input type="text" name="email" />
            电话 <input type="text" name="phone" /> <br/>
            <input type="button" value="提交" id="addBtn" />
        </form>
        <hr/> 
        <table border="1">
            <tr>
                <th>姓名</th>
                <th>email</th>
                <th>电话</th>
                <th>删除</th>
            </tr>
        </table>
    </div>
</body>
</html>

 

jquery动态添加表单数据

原文:http://www.cnblogs.com/mr-wuxiansheng/p/6415726.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!