form-serialize.js --- 表单数据快速收集插件

1. 下载

官方网站

https://www.npmjs.com/package/form-serialize

1. npm下载

npm install form-serialize

2. 收集表单数据


<form id="example-form">
    <input type="text" name="foo" value="bar"/>
    <input type="submit" value="do it!"/>
</form>
<script src="./form-seralize/index.js"></script>
<script>
    const form = document.querySelector('#example-form');  

    const obj1 = serialize(form, {hash: true, empty: true});   // 将form表单收集成 {foo:"bar"} 对象
    console.log(obj)

    const str = serialize(form);   // 将form表单收集成 "foo=bar" 查询字符串
    const obj2 = serialize(form, {hash: false, empty: true});   // hash默认为false,将form表单收集成  "foo=bar" 查询字符串
    const obj3 = serialize(form, {hash: false, empty: false});   //empty默认为false,不获取表单中的空值

    form.reset()
</script>

3. 清空表单数据

const form = document.querySelector('#example-form');  
form.reset()
posted @ 2024-05-25 18:05  河图s  阅读(452)  评论(0)    收藏  举报