SingleQuestionView.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <div class="question-view">
  3. <question-body :questionBody="question.body" :examQuestionId="examQuestion.id"></question-body>
  4. <div class="ops">
  5. <div class="stu-answer">{{stuAnswer}}</div>
  6. <div class="score">({{question.questionScore}}分)</div>
  7. </div>
  8. <div v-for="(option, index) in question.options" :key="option.id" class="option" @click="answerQuestion(examQuestion.id, optionName[index])">
  9. <input type="radio" name="question" value="optionName[index]" :checked="stuAnswer === optionName[index]" />
  10. <span style="padding: 0 10px;">{{optionName[index]}}: </span>
  11. <span class="question-options" v-html="option.content"></span>
  12. </div>
  13. <div class="reset">
  14. <i-button type="warning" size="large" @click="resetQuestion(examQuestion.id)">重置答案</i-button>
  15. </div>
  16. </div>
  17. </template>
  18. <script>
  19. import QuestionBody from "./QuestionBody";
  20. const optionName = ["A", "B", "C", "D", "E", "F"];
  21. export default {
  22. name: "SingleQuestionView",
  23. data() {
  24. return {
  25. optionName,
  26. stuAnswer: this.examQuestion.stuAnswer
  27. };
  28. },
  29. props: {
  30. question: Object,
  31. examQuestion: Object
  32. },
  33. created: function() {
  34. window.addEventListener("keyup", this.keyup);
  35. },
  36. destroyed() {
  37. window.removeEventListener("keyup", this.keyup);
  38. },
  39. methods: {
  40. keyup(e) {
  41. // console.log(e);
  42. // console.log(document.activeElement.type);
  43. if (
  44. ["BODY", "A", "BUTTON", "DIV"].includes(
  45. document.activeElement.tagName
  46. ) ||
  47. (["INPUT"].includes(document.activeElement.tagName) &&
  48. ["checkbox", "radio"].includes(document.activeElement.type))
  49. ) {
  50. if (
  51. ["KeyA", "KeyB", "KeyC", "KeyD", "KeyE", "KeyF", "KeyG"]
  52. .slice(0, this.question.options.length)
  53. .includes(e.code)
  54. ) {
  55. this.stuAnswer = e.code[3];
  56. this.answerQuestion(this.examQuestion.id, this.stuAnswer);
  57. }
  58. }
  59. },
  60. answerQuestion: function(examQuestionId, stuAnswer) {
  61. this.stuAnswer = stuAnswer;
  62. this.$http.put("/api/exam_question/" + examQuestionId, { stuAnswer });
  63. },
  64. resetQuestion: function(examQuestionId) {
  65. this.stuAnswer = null;
  66. this.$http.put("/api/exam_question/" + examQuestionId, {
  67. stuAnswer: null
  68. });
  69. }
  70. },
  71. watch: {
  72. examQuestion: function() {
  73. this.stuAnswer = this.examQuestion.stuAnswer;
  74. }
  75. },
  76. components: {
  77. QuestionBody
  78. }
  79. };
  80. </script>
  81. <style scoped>
  82. .question-view {
  83. display: grid;
  84. grid-row-gap: 10px;
  85. }
  86. .question-body {
  87. font-size: 18px;
  88. margin-bottom: 10px;
  89. }
  90. .ops {
  91. display: flex;
  92. align-items: flex-end;
  93. }
  94. .stu-answer {
  95. width: 100px;
  96. border-bottom: 1px solid black;
  97. text-align: center;
  98. height: 20px;
  99. }
  100. .option {
  101. display: flex;
  102. }
  103. .question-options {
  104. text-align: left;
  105. }
  106. </style>