29.Vue训练v_model收集表单数据

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script type="text/javascript" src="./vue.js"></script>
		<style type="text/css">
			label {
				-ms-user-select: none;
				-moz-user-select: none;
				-webkit-user-select: none;
			}
		</style>
	</head>
	<body>
		<div id="root">
			<!-- prevent阻止表单提交默认行为(跳转页面) -->
			<form @submit.prevent="demo">
				<label for="account">账号:</label>
				<!-- trim去除两边空格 -->
				<input type="text" v-model.trim="account" id="account" />
				<br><br>
				<label for="password">密码:</label>
				<input type="password" v-model="password" id="password" />
				<br><br>
				<label for="age">年龄:</label>
				<!-- v-model.number将字符串转为数值 -->
				<input type="number" v-model.number="age" id="age" />
				<br><br>
				性别:
				<input type="radio" v-model="sex" value="男" id="man" name="sex" />
				<label for="man"></label>
				<input value="女" v-model="sex" style="margin-left: 20px;" type="radio" id="lady" name="sex" />
				<label for="lady"></label>
				<br><br>
				爱好:
				<input type="checkbox" v-model="hobby" value="学习" id="study" />
				<label for="study">学习</label>
				<input type="checkbox" v-model="hobby" value="打游戏" id="play_game" />
				<label for="play_game">打游戏</label>
				<input type="checkbox" v-model="hobby" value="吃饭" id="eat" />
				<label for="eat">吃饭</label>
				<br><br>
				所选校区
				<select v-model="city">
					<option value="">请选择校区</option>
					<option value="beijing">北京</option>
					<option value="shanghai">上海</option>
					<option value="wuhan">武汉</option>
				</select>
				<br><br>
				其他信息:
				<!-- v-model.lazy失去焦点时收集数据 -->
				<textarea v-model.lazy="other" ></textarea>
				<br><br>
				<input v-model="agree" type="checkbox">阅读并接受<a href="https://www.baidu.com/">《用户协议》</a>
				<button type="submit">提交</button>
			</form>
		</div>
	</body>
</html>

<script type="text/javascript">
	const userInfo = {
		account: '',
		password: '',
		sex: '男',
		age: 19,
		hobby: [],
		city: 'beijing',
		other: '',
		agree: false
	};
	const vm = new Vue({
		el: '#root',
		data: function() {
			return userInfo;
		},
		methods: {
			demo() {
				console.log(JSON.stringify(userInfo));
			}
		}
	});
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

修罗_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值