<script type="text/javascript" src="jquery.min.js"></script> <script type="text/javascript" src="jquery.jput.js"></script>接着我需要定义一个表单,用于输入数据:
<form id="myForm"> <label for="name">姓名:</label> <input type="text" name="name" /> <br /> <label for="age">年龄:</label> <input type="text" name="age" /> <br /> <label for="gender">性别:</label> <input type="radio" name="gender" value="男" />男 <input type="radio" name="gender" value="女" />女 <br /> <button id="submitBtn" type="submit">提交</button> </form>然后我需要定义模板,即表格的样式:
<table id="myTable">
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{name}}</td>
<td>{{age}}</td>
<td>{{gender}}</td>
</tr>
</tbody>
</table>最后我需要在JS文件中对Jput进行配置:$(function () {
$("#submitBtn").click(function (event) {
event.preventDefault();
$("#myTable tbody").jput({
data: $("#myForm").serializeArray(),
tmpl: "#myTable >tbody >tr:first"
});
});
});其中,data属性指定表单数据,tmpl属性指定表格的样式。这里,我通过jQuery选择器选中了table元素中的第一个tbody,由于这个tbody中只有一行,所以我指定了tmpl选择器为tbody中的第一行。
好了,这就是Jput的使用,现在你可以自行尝试一下,它一定会给你带来很大的便利。
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did236568