javascript获取form数据

访问 form:

document.forms[0]

document.forms[‘myFormName’]

document.forms.myFormName

myFormName

myFormName.elements 可以访问到所有的表单元素

获取表单数据:

<form name="myform" ...>
  <input type="text" name="username" >

  <input type="checkbox" name="cbdata" >
  <input type="checkbox" name="cbdata" >
  ...
</form>

//获取 username
let formData = new FormData(myform)
let userName = formData.get('username')
//获取 checkbox
let allCbData = formData.getAll('cbdata')

很简洁的方法。


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

two + 2 =