Jquery文档中serialize()方法的描述为:序列表表格内容为字符串。
以下是测试代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>Jquery实现只提交部分Form表单数据</title> <meta name="description" content=""> <meta name="keywords" content=""> <style type="text/css"> .form {padding:15px;border: #FFE1D3 2px solid; background-color:#FFF0E7;border-radius: 3px;} .section {position: relative;border-bottom: #FFE1D3 1px solid;} .section .save {position: absolute; left:196px; top: 0;color: #2473CF;text-decoration: none;} </style> <script src="./js/jquery.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function(){ $(‘.save‘).click(function(){ var a = $(this); var section = a.closest(‘.section‘); var f = $(‘<form></form>‘), newForm = section.clone(); f.append(newForm); var sectionData = f.serialize(); $.ajax({ url: a.attr(‘data-url‘), type: "POST", dataType: ‘json‘, data: sectionData, success: function(data){ console.log(data); }, error:function(){ } }); }); }); </script> </head> <body> <div class="form"> <form method="post" action=""> <div class="section"> <a class="save" href="javascript:void(0);" data-url="formAction.php">保存</a> <p><input type="text" name="username" placeholder="用户名"></p> <p><input type="text" name="email" placeholder="Email"></p> </div> <div class="section"> <a class="save" href="javascript:void(0);" data-url="formAction.php">保存</a> <p><input type="text" name="age" placeholder="年龄"></p> <p> <select name="gender"> <option value="m">男</option> <option value="f">女</option> </select> </p> </div> <div class="section"> <input type="submit" value="提交"> </div> </form> </div> </body> </html>
分别点击保存,可以看到两次提交的内容的不同。
时间: 2024-10-19 09:55:17