~Vue实现简单答题功能,主要包含单选框和复选框
内容
实现简单答题效果
环境
Vue,webpack(自行安装)
实现方式
页面将答题列表传递给调用组件,组件将结果返回给调用页面(其它模式也ok,这只是例子)
--------------------------------------------分割线-----------------------------------------------
组件 zujian8.vue
<template>
<div class="aaaa">
<div class="div" v-for="(son,index) in list_a" :key="index">
<div class="question">问题:{{son.question }}</div>
<div class="type">类型:{{son.type=== 1 ? '单选' : '多选' }}</div>
<div v-if="son.type === 1" class="answer">
<li v-for="(sson,index1) in son.answer" :key="index1" >
<span>{{sson.value}}</span>
<input type="radio" :name="son.name" :value="sson.value" @change="get_radio_value(index)" v-model="checkedValue[index]" >
</li>
<div style="clear: both"></div>
</div>
<div v-else class="answer">
<li v-for="(sson,index1) in son.answer" :key="index1">
<span>{{sson.value}}</span>
<input type="checkbox" :name="son.name" :value="sson.value" @change="get_checkbox_value(index)" v-model="checkedValue1" >
</li>
</div>
<hr>
</div>
<button @click="btnfun">提交</button>
</div>
</template> <script>
export default {
name: 'input8',
data: function () {
return {
all_list: [],
checkedValue: [], // 绑定单选框的值
checkedValue1: [] // 绑定复选框的值
}
},
props: ['list_a'],
methods: {
btnfun: function () {
// 获取input框的值
console.log(this.all_list)
// 如果答案长度不匹配list_a
// this.all_list = this.all_list.null
// console.log(this.all_list)
for (var i = 0; i < this.all_list.length; i++) {
if (this.all_list[i] === '' || typeof (this.all_list[i]) === 'undefined') {
this.all_list.splice(i, 1)
}
}
// 循环
if (this.list_a.length !== this.all_list.length) {
console.log('答案没有选择完毕')
} else {
console.log('答案选择完毕')
// 传值给调用页面
this.$emit('transfer', this.all_list)
}
},
get_radio_value: function (index) {
// 获取当前radio当前值
console.log((index + 1) + '题' + this.checkedValue)
this.all_list[index] = this.checkedValue[index]
},
get_checkbox_value: function (index) {
// 获取当前复选框的值
console.log((index + 1) + '题' + this.checkedValue1)
this.all_list[index] = this.checkedValue1
}
}
}
</script>
<style scoped>
li{
list-style: none;
}
.div{
margin: 6px 0px;
}
.question {
width:300px;
text-align: left;
}
.type{
width:200px;
text-align: left;
}
.answer li{
width:100%;
height: 20px;
}
.answer span{
float: left;
}
.answer input{
float: right;
}
</style>
调用页面 test.vue
该页面较长我就复制关键部分的代码
--------------------------------------------分割线-----------------------------------------------
组件引入
import input8 from './zujian8' //引入
组件注册
components: {
input8
},
初始化答案数据
data () {
return {
list_a: [
{'type': 1, 'name': 'one', 'question': 'Are you programmer?', 'answer': [{'value': 'A.Yes'}, {'value': 'B.No'}]},
{'type': 1, 'name': 'two', 'question': 'Are you a man?', 'answer': [{'value': 'A.Of course'}, {'value': 'B.No'}]},
{'type': 1, 'name': 'three', 'question': 'Are you a Staff?', 'answer': [{'value': 'A.Yes'}, {'value': 'B.No'}]},
{'type': 1, 'name': 'four', 'question': 'Are you in sichuan?', 'answer': [{'value': 'A.Yes'}, {'value': 'B. No'}]},
{'type': 2, 'name': 'five', 'question': 'How about your in skills?', 'answer': [{'value': 'A.Python'}, {'value': 'B.Vue'}, {'value': 'C.Php'}, {'value': 'D.Java'}]}
]
}
}
调用组件,接收组件返回值
<div class="xxxx">
<input8 :list_a="list_a" @transfer = 'postAnswer'/>
<!--<button @click="buttonFun">提交</button>-->
</div>
定义函数
methos: {
postAnswer: function (msg) {
console.log(msg)
}
}
页面截图
如上图所示。
~Vue实现简单答题功能,主要包含单选框和复选框的更多相关文章
- vue.js实现单选框、复选框和下拉框
Vue.js可以很方便的实现数据双向绑定,所以在处理表单,人机交互方面具有很大的优势.下边以单选框.复选框和下拉框为例介绍他们在HTML和Vue.js中的具体实现方式. 一.单选框 在传统的HTM ...
- CSS学习笔记三:自定义单选框,复选框,开关
一点一点学习CCS,这次学习了如何自定义单选框,复选框以及开关. 一.单选框 1.先写好body里面的样式,先写几个框 <body> <div class="radio-1 ...
- bootstrap-multiselect 设置单选无效(设置单选依然是复选框)
bootstrap-multiselect 的使用介绍:https://www.cnblogs.com/landeanfen/p/5013452.html bootstrap-multiselect ...
- Flutter 基础组件:单选框和复选框
前言 Material组件库中提供了Material风格的单选开关Switch和复选框Checkbox,虽然它们都是继承自StatefulWidget,但它们本身不会保存当前选中状态,选中状态都是由父 ...
- Selenium_单选框和复选框的选中状态判定以及元素是否可用和可见判定(10)
简单写个单选框和复选框界面 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /&g ...
- Selenium3自动化测试【28】单选框、复选框、下拉选择框
Html页面中的单选按钮.复选框.下拉框均可通过WebDriver实现操做.本节结合案例一起来看看WebDriver如何操做这些控件. 同步视频知识与系列知识内容,可关注:[公众号]:柒哥测试:[WX ...
- 自动化测试-15.selenium单选框与复选框状态判断
本篇主要介绍单选框和复选框的操作 一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了 ...
- 2.12 单选框和复选框(radiobox、checkbox)
2.12 单选框和复选框(radiobox.checkbox) 本篇主要介绍单选框和复选框的操作一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是 ...
- Selenium2学习(十五)-- 单选框和复选框(radiobox、checkbox)
本篇主要介绍单选框和复选框的操作 一.认识单选框和复选框 1.先认清楚单选框和复选框长什么样 2.各位小伙伴看清楚哦,上面的单选框是圆的:下图复选框是方的,这个是业界的标准,要是开发小伙伴把图标弄错了 ...
- 微信小程序-修改单选框和复选框大小的方法
方法有两种: 一:采用css的zoom属性 zoom缩放会将元素保持在左上角,并且会有毛边,可能会稍稍改变元素原来的形状. 二:采用css3的transform:scale属性 zoom缩放会将元素保 ...
随机推荐
- 打印N个数的循环算法和递归算法比较
1.循环算法: void PrintN_1(int N) { int i; ; i <= N; i++) printf("%d\n", i); return; } N可以为任 ...
- sqoop上传数据到hdfs,并用hive管理数据。
sqoop导入mysql数据表到HDFS中sqoop import --connect jdbc:mysql://master:3306/test --username root --password ...
- monkey如何获取app包名
别人学习网址:http://www.51testing.com/html/58/15092658-2984032.html 使用aapt aapt是sdk自带的一个工具,在sdk\builds- ...
- jsp中如何清除缓存(转)
<% response.setHeader("Cache-Control","no-cache"); //HTTP 1.1 response.setHea ...
- dojo DataGrid实现表格数据编辑的解决方案
在官网上看见的DataGrid编辑非常简单,但我实现的时候总是出现问题.经过N久的摸索,终于搞定了,期间出现了多处困难,下面说些解决办法的流程. 我实现的表格在页面加载时是不显示数据,只有通过表单像服 ...
- 【设计模式】JDK源码中用到的设计模式
https://blog.csdn.net/angjunqiang/article/details/42061453 https://blog.csdn.net/baiye_xing/article/ ...
- ThreadPoolExecutor简单学习
Executors和ThreadPoolExecutor两者的区别和联系 jdk中文文档 https://blog.fondme.cn/apidoc/jdk-1.8-google/ 还可以的两个博客 ...
- 【正则表达式】使用正则表达式的group,查找出String中的参数值
需求 指标基本格式: clm.{type}.{hostId}.$metricItem 示例1: // 待匹配表达式:<hostId: 为36位的UUID> summarize(clm.pm ...
- SpringMVC---400错误The request sent by the client was syntactically incorrect ()
在SpringMVC中使用@RequestBody和@ModelAttribute注解时遇到了很多问题,现记录下来. @ModelAttribute这个注解主要是将客户端请求的参数绑定参数到一个对象上 ...
- 【IIS错误】未能加载文件或程序集“AAAAA”或它的某一个依赖项。试图加载格式不正确的程序。
未能加载文件或程序集“AAAAA”或它的某一个依赖项.试图加载格式不正确的程序. 说明: 执行当前 Web 请求期间,出现未经处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的 ...