form表单设置成json格式
时间: 2023-12-26 13:28:44 浏览: 54
根据提供的引用内容,你可以使用JavaScript或Vue.js来将表单数据转换为JSON格式。下面是一个使用Vue.js的例子:
```html
<template>
<div>
<form @submit.prevent="submitForm">
<input type="text" v-model="name" placeholder="Name">
<input type="email" v-model="email" placeholder="Email">
<button type="submit">Submit</button>
</form>
</div>
</template>
<script>
export default {
data() {
return {
name: '',
email: ''
}
},
methods: {
submitForm() {
const formData = {
name: this.name,
email: this.email
}
const jsonData = JSON.stringify(formData)
console.log(jsonData)
}
}
}
</script>
```
在上面的例子中,我们使用了Vue.js的v-model指令来绑定表单输入的值到Vue实例的data属性中。当表单提交时,我们将数据存储在一个formData对象中,并使用JSON.stringify方法将其转换为JSON格式的字符串。最后,我们将JSON数据打印到控制台。