17-Vue核心-收集表单数据
收集表单数据
1)若:<input type="text"/>,则v-model收集的是value值,用户输入的就是value值
<input type="text" id="account" placeholder="请输入账号信息" v-model="userInfo.accountData">
2)若:<input type="radio"/>,则v-model收集的是value值,且要给标签配置value值
男<input type="radio" name="sex" v-model="userInfo.sex" value="male">
女<input type="radio" name="sex" v-model="userInfo.sex" value="female">
3)若:<input type="checkbox"/>
- 没有配置input的value属性,那么收集的就是checked(勾选 或 未勾选,是布尔值)
- 配置input的value属性
a) v-model的初始值是非数组,那么收集的就是check(勾选 或 未勾选,是布尔值)
b) v-model的初始值是数组,那么收集的就是value组成的数组
学习<input type="checkbox" name="hobby" v-model="userInfo.hobby" value="study"> 运动<input type="checkbox" name="hobby" v-model="userInfo.hobby" value="motion"> 打游戏<input type="checkbox" name="hobby" v-model="userInfo.hobby" value="game">
v-model的三个修饰符
lazy:失去焦点再收集数据
number:输入字符串转为有效的数据 (通常这样搭配,<input type="number" v-model.number="xxx">)
trim:输入首位空格过滤
第一种方式收集表单数据
直接使用this._data调用数据
data(){ return{ accountData:"", passwordData:"", // 单选框,默认选择male sex:"male", hobby:[], city:"", other:"", agree:"", } }
// 使用 JSON.stringify() 方法将 JavaScript 对象转换为字符串 console.log(JSON.stringify(this._data))
第二种方式收集表单数据(在开发过程中更常见)
将表单数据封装到一个userInfo对象中,然后再使用this.userInfo调用数据,但这里也需要在Vue绑定样式时也进行相应修改
data(){ return{ userInfo:{ accountData:"", passwordData:"", sex:"male", hobby:[], city:"", other:"", agree:"", } } }
// 使用 JSON.stringify() 方法将 JavaScript 对象转换为字符串 console.log(JSON.stringify(this.userInfo))
完整代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>收集表单数据</title>
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<!--准备好一个容器-->
<div id="root">
<h2>表单信息</h2>
<!--表单Form-->
<!--表单绑定提交事件(事件修饰符prevent:阻止默认事件) 表单提交时,绑定demo方法-->
<form @submit.prevent="demo">
<label for="account">账号:</label>
<!--v-model修饰符 trim 可以将输入首尾的空格过滤-->
<!--<input type="text" id="account" placeholder="请输入账号信息" v-model.trim="accountData"><br/><br/>-->
<input type="text" id="account" placeholder="请输入账号信息" v-model.trim="userInfo.accountData"><br/><br/>
<label for="password">密码:</label>
<!--<input type="password" id="password" placeholder="请输入密码" v-model="passwordData"><br/><br/>-->
<input type="password" id="password" placeholder="请输入密码" v-model="userInfo.passwordData"><br/><br/>
年龄:
<!--v-model修饰符 number 可以将输入字符串转为有效的数字-->
<!--<input type="number" v-model.number="age"><br/><br/>-->
<input type="number" v-model.number="userInfo.age"><br/><br/>
性别:
<!--男<input type="radio" name="sex" value="male" checked="checked">-->
<!--男<input type="radio" name="sex" v-model="sex" value="male">-->
<!--女<input type="radio" name="sex" v-model="sex" value="female"><br/><br/>-->
男<input type="radio" name="sex" v-model="userInfo.sex" value="male">
女<input type="radio" name="sex" v-model="userInfo.sex" value="female"><br/><br/>
爱好:
<!--学习<input type="checkbox" name="hobby" v-model="hobby" value="study">-->
<!--运动<input type="checkbox" name="hobby" v-model="hobby" value="motion">-->
<!--打游戏<input type="checkbox" name="hobby" v-model="hobby" value="game"><br/><br/>-->
学习<input type="checkbox" name="hobby" v-model="userInfo.hobby" value="study">
运动<input type="checkbox" name="hobby" v-model="userInfo.hobby" value="motion">
打游戏<input type="checkbox" name="hobby" v-model="userInfo.hobby" value="game"><br/><br/>
所属校区
<select v-model="userInfo.city">
<option value="">请选择所属校区</option>
<option value="beijing">北京</option>
<option value="tianjin">天津</option>
<option value="shanghai">上海</option>
<option value="shenzhen">深圳</option>
</select>
<br/><br/>
其它信息:
<!--v-model修饰符 lazy 失去焦点后再收集数据-->
<!--<textarea rows="8" v-model="other"></textarea><br/><br/>-->
<textarea rows="8" v-model.lazy="userInfo.other"></textarea><br/><br/>
<!--<input type="checkbox" v-model="agree" name="agree">-->
<input type="checkbox" v-model="userInfo.agree" name="agree">
<阅读并接受<a href="http://www.baidu.com">《用户协议》</a><br/><br/>
<button>提交</button>
</form>
</div>
<script type="text/javascript">
// 阻止 vue 在启动时生成生产提示
Vue.config.productionTip = false
const vm = new Vue({
el:"#root",
data(){
return{
// accountData:"",
// passwordData:"",
// // 单选框,默认选择male
// age:""
// sex:"male",
// hobby:[],
// city:"",
// other:"",
// agree:"",
userInfo:{
accountData:"",
passwordData:"",
age:"",
sex:"male",
hobby:[],
city:"",
other:"",
agree:"",
}
}
},
methods:{
demo(){
// 使用 JSON.stringify() 方法将 JavaScript 对象转换为字符串
// console.log(JSON.stringify(this._data))
console.log(JSON.stringify(this.userInfo))
}
}
})
</script>
</body>
</html>


浙公网安备 33010602011771号