form-value

1.0.5 • Public • Published

form-value

简体中文

Introduction

Does not rely on any libraries or frameworks, Use only the native DOM.

val() function is provided,you can use val(form) gets the form values and use val(form, data) assign a value to the form item.

Install

npm i form-value

and in browser

<script src="./browser/form-value.js"></script>

Gets the form values

first, you must be set every item name attribute.

let form = document.querySelector('form')

let data = val(form)
/**
returns
{
  key: value,
  key: value,
  key: [value1, value2], // if form item type is checkbox
  ...
}
*/

// if you want to get querystring
let data = val(form, 'querystring')
/**
returns   key=value&key=value...
*/

// if you want to get FormData
let data = val(form, 'formdata')
/**
returns the FormData Object
*/

Assign

You need to pass in a literal object, and the key of the object must be equal to the name attribute of the form item.

supported DOMStringMap (HTMLElement.dataset) type since 1.0.4

let data = {
  text: 'hello world',
  password: 123456,
  sex: 'M'
}
let form = document.querySelector('form')

val(form, data) // Set default values for form entries

Package Sidebar

Install

npm i form-value

Weekly Downloads

1

Version

1.0.5

License

ISC

Unpacked Size

17.3 kB

Total Files

7

Last publish

Collaborators

  • langzilaotang