MultipleQuestionView.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <div class="question-view">
  3. <div class="question-body" v-html="question.body"></div>
  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="toggleAnswer(index)">
  9. <input type="checkbox" name="question" value="optionName[index]" :checked="stuAnswer && stuAnswer.includes(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. const optionName = ["A", "B", "C", "D", "E", "F"];
  20. export default {
  21. name: "MultipleQuestionView",
  22. data() {
  23. return {
  24. optionName,
  25. stuAnswer: this.examQuestion.stuAnswer || ""
  26. };
  27. },
  28. props: {
  29. question: Object,
  30. examQuestion: Object
  31. },
  32. created: function() {
  33. window.addEventListener("keyup", this.keyup);
  34. },
  35. destroyed() {
  36. window.removeEventListener("keyup", this.keyup);
  37. },
  38. methods: {
  39. keyup(e) {
  40. // console.log(e);
  41. // console.log(document.activeElement.type);
  42. if (
  43. ["BODY", "A", "BUTTON", "DIV"].includes(
  44. document.activeElement.tagName
  45. ) ||
  46. (["INPUT"].includes(document.activeElement.tagName) &&
  47. ["checkbox", "radio"].includes(document.activeElement.type))
  48. ) {
  49. if (
  50. ["KeyA", "KeyB", "KeyC", "KeyD", "KeyE", "KeyF", "KeyG"]
  51. .slice(0, this.question.options.length)
  52. .includes(e.code)
  53. ) {
  54. if (this.stuAnswer.includes(e.code[3])) {
  55. this.stuAnswer = this.stuAnswer.replace(e.code[3], "");
  56. } else {
  57. this.stuAnswer = this.stuAnswer
  58. .concat(e.code[3])
  59. .split("")
  60. .sort()
  61. .join("");
  62. }
  63. }
  64. }
  65. },
  66. toggleAnswer: function(index) {
  67. if (this.stuAnswer.includes(optionName[index])) {
  68. this.stuAnswer = this.stuAnswer.replace(optionName[index], "");
  69. } else {
  70. this.stuAnswer = this.stuAnswer
  71. .concat(optionName[index])
  72. .split("")
  73. .sort()
  74. .join("");
  75. }
  76. this.answerQuestion();
  77. },
  78. answerQuestion: function() {
  79. this.$http.put("/api/exam_question/" + this.examQuestion.id, {
  80. stuAnswer: this.stuAnswer
  81. });
  82. },
  83. resetQuestion: function(examQuestionId) {
  84. this.stuAnswer = "";
  85. this.$http.put("/api/exam_question/" + examQuestionId, {
  86. stuAnswer: null
  87. });
  88. }
  89. },
  90. watch: {
  91. examQuestion: function() {
  92. this.stuAnswer = this.examQuestion.stuAnswer;
  93. }
  94. }
  95. };
  96. </script>
  97. <style scoped>
  98. .question-view {
  99. display: grid;
  100. grid-row-gap: 10px;
  101. }
  102. .question-body {
  103. font-size: 18px;
  104. margin-bottom: 10px;
  105. }
  106. .ops {
  107. display: flex;
  108. align-items: flex-end;
  109. }
  110. .stu-answer {
  111. width: 100px;
  112. border-bottom: 1px solid black;
  113. text-align: center;
  114. height: 20px;
  115. }
  116. .option {
  117. display: flex;
  118. }
  119. .question-options {
  120. text-align: left;
  121. }
  122. </style>