<!doctype html> <html> <head> <meta charset="UTF-8"> <title>把一些表单属性封装一个JSON</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/> <style> </style> </head> <body> <h1>员工查询</h1> <label>请输入员工编号:</label> <input type="text" id="keyword" value="dd"/> <button id="search">查询</button> <p id="searchResult"></p> <h1>员工新建</h1> <label>请输入员工姓名:</label> <input type="text" id="staffName"/><br> <label>请输入员工编号:</label> <input type="text" id="staffNumber"/><br> <label>请选择员工性别:</label> <select id="staffSex"> <option>女</option> <option>男</option> </select><br> <label>请输入员工职位:</label> <input type="text" id="staffJob"/><br> <input type="hidden" id="oDiv"> <button id="save">保存</button> <p id="createResult"></p> <script type="text/javascript"> function d(id) { return document.getElementById(id); } var fdata = {}; fdata.oname = d("staffName").value; fdata.onumber = d("staffNumber").value; fdata.osex = d("staffSex").value; fdata.ojob = d("staffJob").value; var jsonArr = JSON.stringify(fdata); d("oDiv").setAttribute("name", jsonArr); </script> </body> </html>