VUE 单选下拉框Select中动态加载 默认选中第一个
2024-08-31 11:57:57
<lable>分类情况</lable>
<select v-model="content.tid">
<option v-for="type in types" :value="type.id" :key="type.id">
{{type.name}}
</option>
</select>
总结的很到位
vue select下拉框绑定默认值:
首先option要加value值,以便v-model可以获取到对应选择的值
一、当没有绑定v-model,直接给对应的option加selected属性
二、当给select绑定了v-model的值的时候,要给v-model绑定的data值里写默认值
<template>
<div id ="learn-insert">
<h1>新增学习记录</h1>
<form v-if="!submmited">
<p>学习内容标题</p>
<input type="text" v-model="content.desc" required /> <p>学习详细内容</p>
<textarea type="text" v-model="content.details" required></textarea> <p>分类情况</p>
<select v-model="content.tid">
<option v-for="type in types" :value="type.id" :key="type.id">
{{type.name}}
</option> </select> <button v-on:click.prevent="post">添加学习内容</button>
</form> <div v-if="submmited" >
<h3>亲的学习内容已经发布成功</h3>
</div> <div id="preview">
<h3>内容概览</h3>
<p> 内容描述: {{content.desc}}</p>
<p> 内容详细描述: {{content.details}}</p>
<p> 内容详细描述: {{content.tid}}</p>
</div> </div>
</template> <script>
export default {
data() {
return {
content:{
id:"",
tid:"1",
uid:"",
datetime:"",
desc:"",
details:""
},
types:[],
submmited: false,
id:"1"
}
},
methods: {
post: function(){
this.$http.post('http://localhost:8085/content/add', this.content)
.then((data) =>{
console.log(data);
console.log(this.submmited);
this.submmited= true;
console.log(this.submmited);
}); } },
created(){
this.$http.get('http://localhost:8085/type/showall', true)
.then(function(response){
this.types=response.data;
console.log(this.types);
})
//,this.content.id=this.types[0].id }
} </script> <style scoped>
/*
#learn-insert *{
box-sizing: border-box;
} #learn-insert{
margin: 20px auto;
max-width: 600px;
padding: 20px;
}
*/
label{
display: block;
margin: 20px 0 10px;
} input[type="text"],textarea,select{
display: block;
width: 100%;
padding: 8px;
} textarea{
height: 200px;
} #checkboxes label{
display: inline-block;
margin-top: 0;
} #checkboxes input{
display: inline-block;
margin-right: 10px;
} button{
display: block;
margin: 20px 0;
background: crimson;
color: #fff;
border: 0;
padding: 14px;
border-radius: 4px;
font-size: 18px;
cursor: pointer;
} #preview{
padding: 10px 20px;
border: 1px dotted #ccc;
margin: 30px 0;
} h3{
margin-top: 10px;
}
</style>
最新文章
- linux内核(kernel)版本号的意义
- SDWebImage 加载网络图片失败,重新运行,就能加载成功。
- jacob 多个web项目报错 jacob-1.14.3-x64.dll already loaded in another classloader jacob
- innodb insert buffer 插入缓冲区的理解
- 【BZOJ3504】危桥(最大流)
- 【wikioi】1191 数轴染色(线段树+水题)
- Android 监听键盘的弹起与收缩
- Oracle使用goldengate分别向Oracle和mysql双路的单向复制
- IOS详解TableView——对话聊天布局的实现
- apply/call/bind和this的使用
- 瓦片切图工具gdal2tiles.py改写为纯c++版本
- 搭建idea下的vue工程
- Sql Server中sql语句自己主动换行
- 加密入门(三):TrueCrypt(转)
- Python Extension Packages 下载
- zw版【转发&#183;台湾nvp系列Delphi例程】HALCON HistoToThresh1
- Hibernate 中的锁( locking )
- JS判定注册表单的几个方式 及 Ajax进行用户名存在判定
- CTF中的变量覆盖漏洞
- AspNet.WebAPI.OData.ODataPQ实现WebAPI的分页查询服务-(个人拙笔)(转)
热门文章
- Java实现回形数,只利用数组、循环和if-else语句
- www.zhaoyueyi.cn
- 排序算法的实现之Javascript(常用)
- MySQL 事务一览
- golang 切片扩容, 时间复杂度
- RabbitMQ的安装(Windows环境下)
- [转帖]详解shell脚本括号区别--$()、$「 」、$「 」 、$(()) 、「 」 、「[ 」]
- 给出一个javascript的Helloworld例子
- 什么是SSH 以及常见的ssh 功能
- TypeScript 高级类型 接口(interface)