欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vue.js动态添加、删除选题的实例代码

程序员文章站 2022-06-20 11:57:19
大家先看看页面效果吧,当当当当``````````````````````` 图中第二个选题是小颖点击了"新增选题"按钮,然后出来的,当你点击了"删除选项"或"删除选...

大家先看看页面效果吧,当当当当```````````````````````

Vue.js动态添加、删除选题的实例代码

图中第二个选题是小颖点击了"新增选题"按钮,然后出来的,当你点击了"删除选项"或"删除选题"按钮,就会删除相应的选项和选题.

html代码

<template>
<div class="main-container">
<div class="form-horizontal">
<template v-for='(subjectindex,subject) in question'>
<div class="form-group">
<label class="col-sm-1 control-label">选题:</label>
<label class="col-sm-2 control-label red remove-pad" v-if='$index>0' @click='deletesubjectfun(subjectindex)'>删除选题</label>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">标题:</label>
<div class="col-sm-2">
<input type="text" class="form-control" placeholder="未填写" v-model='subject.title'>
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">正确答案:</label>
<div class="col-sm-1">
<input type="text" class="form-control answer" placeholder="未填写" readonly="true" v-model='subject.answer'>
</div>
<span class="prompting-message">请在以下选项中,勾选出正确答案</span>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">分值:</label>
<div class="col-sm-1">
<input type="number" min="1" max="100" class="form-control" v-model='subject.score' @keyup='scorefun(subjectindex)'>
</div>
</div>
<div class="form-group">
<label class="col-sm-1 control-label">选项:</label>
</div>
<div class="form-group">
<div class="col-sm-3 mg1">
<ul class="upper-latin">
<template v-for='(opdtindex,opdt) in subject.optionsdata'>
<li>
<input type="checkbox" class="checkbox-answer" v-model='subject.answer' value='{{opdt.id}}'>
<div class="options-div">
<input type="text" class="form-control options-input" placeholder="未填写" v-model='opdt.options'>
<label class="control-label blue remove-pad" v-if='opdtindex==subject.optionsdata.length-1' @click='addnewoptionsfun(subjectindex,opdtindex)'>新增选项</label>
<label class="control-label red remove-pad" v-if='opdtindex>0' @click='deleteoptionsfun(subjectindex,opdtindex)'>删除选项</label>
</div>
</li>
</template>
</ul>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-4">
<button class="blue boder" @click='addnewsubjectfun(subjectindex)'>新增选题</button>
</div>
</div>
<div class='split-line'></div>
</template>
<div class="form-group">
<div class="col-sm-offset-1 col-sm-4">
<button class="btn btn-primary" @click='savequestionfun'>提交</button>
</div>
</div>
</div>
</div>
</template>

js代码

<script>
export default {
components: {
},
props: {},
methods: {
// 新增选题
addnewsubjectfun: function(index) {
var subjectdatames = {};
subjectdatames.id = index+2;
subjectdatames.title = '';
subjectdatames.answer =[];
subjectdatames.score = 10;
subjectdatames.optionsdata = [{
id: 'a',
options: ''
}];
this.question.push(subjectdatames);
},
// 判断每个题目的分值不能小于零且不能大于一百
scorefun:function(index){
if(this.question[index].score<0){
this.question[index].score=10;
this.$root.tipstr = '选题"'+this.question[index].title+'"的分值不能小于零';
}else if(this.question[index].score>100){
this.question[index].score=10;
this.$root.tipstr = '选题"'+this.question[index].title+'"的分值不能大于一百';
}
},
// 删除选题
deletesubjectfun: function(index) {
this.question.splice(index, 1);
},
// 新增选项
addnewoptionsfun: function(subjectindex, opdtindex) {
var optionsdatames = {};
optionsdatames.id =string.fromcharcode(64 + (opdtindex+2)); //将id从数字转换成大写字母.
optionsdatames.options = '';
var subjectdatames = this.question[subjectindex].optionsdata;
subjectdatames.push(optionsdatames);
},
// 删除选项
deleteoptionsfun: function(subjectindex, opdtindex) {
var subjectdatames = this.question[subjectindex].optionsdata;
subjectdatames.splice(opdtindex, 1);
},
savequestionfun:function(){
this.question;debugger;
}
},
ready: function() {
},
data() {
return {
question: [{
id: 1,
title: '',
answer: [],
score:10,
optionsdata: [{
id: 'a',
options: ''
}],
}],
}
}
}
</script>

css代码

<style scoped>
.mg1 {
padding: 8px 0 0 113px;
}
.upper-latin {
list-style-type: upper-latin;
}
input.form-control.options-input {
display: inline-block; /*width: 499.16px;*/
width: 100%;
}
.boder {
border: 1px solid;
background-color: #fff;
border-radius: 4px;
line-height: 34px;
padding: 0 15px;
}
.blue {
color: #2689cd;
text-align: center;
}
label.red {
color: #c9302c;
}
label.remove-pad {
/*padding: 10px 20px 10px 15px;*/
line-height: 34px;
text-align: left;
}
.split-line {
height: 10px;
border-top: 2px dashed #e7e8ec;
width: 900px;
}
.prompting-message {
line-height: 34px;
}
.options-div {
margin-left: 26px;
margin-top: -25px;
padding-bottom: 25px;
}
</style>

以上所述是小编给大家介绍的vue.js动态添加、删除选题的实例代码,希望对大家有所帮助