在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:defunctzombie/form-serialize开源软件地址:https://github.com/defunctzombie/form-serialize开源编程语言:JavaScript 100.0%开源软件介绍:form-serializeserialize form fields to submit a form over ajax installnpm install form-serialize useform-serialize supports two output formats, url encoded (default) or hash (js objects). Lets serialize the following html form: <form id="example-form">
<input type="text" name="foo" value="bar"/>
<input type="submit" value="do it!"/>
</form> var serialize = require('form-serialize');
var form = document.querySelector('#example-form');
var str = serialize(form);
// str -> "foo=bar"
var obj = serialize(form, { hash: true });
// obj -> { foo: 'bar' } apiserialize(form [, options])Returns a serialized form of a HTMLForm element. Output is determined by the serializer used. Default serializer is url-encoded.
options
custom serializerSerializers take 3 arguments: See the example serializers in the index.js source file. notesonly successful control form fields are serialized (with the exception of disabled fields if disabled option is set) multiselect fields with more than one value will result in an array of values in the explicit array fieldsFields who's name ends with This does not affect <form id="example-form">
<input type="checkbox" name="foo[]" value="bar" checked />
<input type="checkbox" name="foo[]" value="baz" />
<input type="submit" value="do it!"/>
</form> var serialize = require('form-serialize');
var form = document.querySelector('#example-form');
var obj = serialize(form, { hash: true });
// obj -> { foo: ['bar'] }
var str = serialize(form);
// str -> "foo[]=bar" indexed arraysAdding numbers between brackets for the array notation above will result in a hash serialization with explicit ordering based on the index number regardless of element ordering. Like the "explicit array fields" this does not affect url-encoding mode output in any way. <form id="todos-form">
<input type="text" name="todos[1]" value="milk" />
<input type="text" name="todos[0]" value="eggs" />
<input type="text" name="todos[2]" value="flour" />
</form> var serialize = require('form-serialize');
var form = document.querySelector('#todos-form');
var obj = serialize(form, { hash: true });
// obj -> { todos: ['eggs', 'milk', 'flour'] }
var str = serialize(form);
// str -> "todos[1]=milk&todos[0]=eggs&todos[2]=flour" nested objectsSimilar to the indexed array notation, attribute names can be added by inserting a string value between brackets. The notation can be used to create deep objects and mixed with the array notation. Like the "explicit array fields" this does not affect url-encoding mode output. <form id="nested-example">
<input type="text" name="foo[bar][baz]" value="qux" />
<input type="text" name="foo[norf][]" value="item 1" />
</form> var serialize = require('form-serialize');
var form = document.querySelector('#todos-form');
var obj = serialize(form, { hash: true });
// obj -> { foo: { bar: { baz: 'qux' } }, norf: [ 'item 1' ] } referencesThis module is based on ideas from jQuery serialize and the Form.serialize method from the prototype library licenseMIT |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论