68 lines
2.7 KiB
JavaScript
68 lines
2.7 KiB
JavaScript
function formatDate(timestamp) {
|
||
if (!timestamp) return '-';
|
||
const date = new Date(timestamp);
|
||
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`;
|
||
}
|
||
|
||
function renderQuestion(question, index) {
|
||
const questionHtml = `
|
||
<div class="question-item">
|
||
<div class="question-item-title">
|
||
${index + 1}. ${question.title}
|
||
${question.required ? '<span style="color: red; margin-left: 5px;">*(必答题)</span>' : ''}
|
||
</div>
|
||
${
|
||
question.type === 'text' ?
|
||
`<div class="question-content">
|
||
<textarea class="text-answer" disabled
|
||
style="width: 100%; min-height: 100px; padding: 8px; margin-top: 10px; resize: vertical; background-color: #f5f5f5;"
|
||
placeholder="此处填写答案"></textarea>
|
||
</div>` :
|
||
`<div class="options">
|
||
${question.options.map(option => `
|
||
<div class="option-item">
|
||
<input type="${question.type}" name="question${index}" disabled>
|
||
<span>${option.content}</span>
|
||
</div>
|
||
`).join('')}
|
||
</div>`
|
||
}
|
||
</div>
|
||
`;
|
||
return questionHtml;
|
||
}
|
||
|
||
function loadQuestionnaire() {
|
||
const urlParams = new URLSearchParams(window.location.search);
|
||
const questionnaireId = urlParams.get('id');
|
||
|
||
if (!questionnaireId) {
|
||
alert('问卷ID不存在!');
|
||
window.location.href = 'myQuestion.html';
|
||
return;
|
||
}
|
||
|
||
const questionnaireList = JSON.parse(localStorage.getItem('questionnaireList') || '[]');
|
||
const questionnaire = questionnaireList.find(q => q.id === questionnaireId);
|
||
|
||
if (!questionnaire) {
|
||
alert('问卷不存在!');
|
||
window.location.href = 'myQuestion.html';
|
||
return;
|
||
}
|
||
|
||
// 填充问卷信息
|
||
document.querySelector('.question-title').textContent = questionnaire.title;
|
||
document.querySelector('.create-time').textContent = formatDate(questionnaire.createTime);
|
||
document.querySelector('.publish-time').textContent = formatDate(questionnaire.publishTime);
|
||
document.querySelector('.end-time').textContent = formatDate(questionnaire.data.endTime);
|
||
document.querySelector('.status').textContent = questionnaire.status;
|
||
|
||
// 渲染问题列表
|
||
const questionListHtml = questionnaire.data.questions
|
||
.map((question, index) => renderQuestion(question, index))
|
||
.join('');
|
||
document.querySelector('.question-list').innerHTML = questionListHtml;
|
||
}
|
||
|
||
window.onload = loadQuestionnaire; |