question/view/myQuestion.html
ddmt c3494cf792 Update questionnaire navigation and enhance functionality
- Changed links in index.html and myQuestion.html to point to the correct pages for better navigation.
- Added new styles and JavaScript functions in createQuestion.html for dynamic question management, including adding, editing, and deleting questions and options.
- Implemented local storage functionality for saving and managing questionnaires, including validation and publishing features.
- Improved user interface elements for better interaction and usability.
2025-01-04 21:08:07 +08:00

467 lines
15 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>我的问卷</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: #C3C3C3;
}
header {
background-color: #CF9A30;
color: #EEE8E4;
text-align: start;
padding: 20px;
display: flex;
}
main {
padding: 30px 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.box {
width: 90%;
padding: 20px;
position: relative;
background-color: #EBEBEB;
box-shadow: 1px 2px 2px rgba(0,0,0,0.4);
}
a{
text-decoration: none;
}
.quesen{
width: 32px;
height: 32px;
font-size: 28px;
font-weight: bold;
text-align: center;
line-height: 24px;
color: #CF9A30;
border-radius: 50px;
padding-top: 5px;
background-color: #EBEBEB;
position: absolute;
font-style: normal;
top: 14px;
left: 60px;
}
.title{
margin-left: 80px;
font-size: 24px;
line-height: 20px;
font-weight: bold;
margin-right: 60px;
}
.nav-bar{
a{
line-height: 20px;
font-size: 20px;
color: #EEE8E4;
border: 2px solid #c78501;
padding: 2px;
}
}
.grid-bar{
width: 85%;
display: flex;
margin-bottom: 10px;
}
.grid-item-title{
color: #2E276D;
font-weight: bold;
span{
margin: 0 8px;
}
padding-right: 20px;
}
.grid-item-add{
color: #EEE8E4;
background: #CF9A30;
font-weight: bold;
padding: 0 5px;
border: 2px solid #c78501;
border-radius: 2px;
position: absolute;
right: 40px;
top: 20px;
z-index: 1;
}
.list-item{
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
list-style: none;
}
.quelist{
width: 100%;
}
.btn-group{
width: 33%;
float: right;
}
.item-title{
/* width: 33%; */
float: left;
}
table {
width: 100%;
border-collapse: collapse;
background: transparent;
box-shadow: none;
}
th, td {
padding: 10px;
text-align: left;
}
.thtitle, .data-row {
display: flex;
justify-content: space-between;
}
.thadd, .data-item {
flex: 0.8;
padding: 0 10px;
}
.btn-row {
display: flex;
flex-wrap: wrap;
gap: 5px;
}
.btn-row button {
padding: 2px 5px;
}
.thtitle {
display: flex;
align-items: center;
}
.thadd {
flex: 0.4;
padding: 0 5px;
white-space: nowrap;
text-align: left;
color: #2E276D;
}
.thadd:last-child {
flex: 1;
}
.data-row {
display: flex;
align-items: center;
}
.data-item {
flex: 0.4;
padding: 0 5px;
text-align: left;
}
.data-item:last-child {
flex: 1;
display: flex;
flex-wrap: wrap;
gap: 5px;
justify-content: flex-start;
}
table th:first-child {
width: 35%;
}
table th:last-child {
width: 65%;
}
tr:not(:first-child) {
background-color: #EBEBEB;
box-shadow: 1px 2px 2px rgba(0,0,0,0.4);
margin-bottom: 10px;
position: relative;
}
tr:not(:first-child) td {
padding: 15px 10px;
border-bottom: 1px solid #C3C3C3;
}
table {
background: transparent;
box-shadow: none;
}
.box {
background: transparent;
box-shadow: none;
}
/* 添加行悬停效果 */
tr:not(:first-child):hover {
background-color: #e0e0e0; /* 稍微深一点的灰色 */
transition: background-color 0.3s ease; /* 添加过渡效果 */
}
</style>
</head>
<body>
<header>
<i class="quesen">?</i>
<div class="title">问卷管理</div>
<div class="nav-bar">
<a href="myQuestion.html">我的问卷</a>
</div>
</header>
<main>
<div class="box">
<div class="grid-item-add" style="cursor: pointer;">+ 新建问卷</div>
<table>
<tr>
<th style="width: 30%;">
<div class="grid-item-title" style="margin-left: 20px;">问卷标题</div>
</th>
<th style="width: 60%;">
<div class="thtitle">
<span class="thadd">创建日期</span>
<span class="thadd">发布日期</span>
<span class="thadd">截止日期</span>
<span class="thadd">问卷状态</span>
<span class="thadd">操作区域</span>
</div>
</th>
</tr>
</table>
<div class="ctrl-all box" style="
background-color: #EBEBEB;
box-shadow: 1px 2px 2px rgba(0,0,0,0.4);
padding: 10px;
width: 100%;
">
<input type="checkbox">
<span>全选</span>
<button>删除问卷</button>
</div>
</div>
</main>
<script>
// 格式化时间戳为日期字符串
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 renderQuestionnaireList() {
// 获取问卷列表数据
const questionnaireList = JSON.parse(localStorage.getItem('questionnaireList') || '[]');
const tableBody = document.querySelector('table');
// 保留表头
const tableHeader = tableBody.rows[0];
tableBody.innerHTML = '';
tableBody.appendChild(tableHeader);
// 渲染每个问卷
questionnaireList.forEach(questionnaire => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>
<div class="item-title">
<input type="checkbox" data-id="${questionnaire.id}">
<span>${questionnaire.title}</span>
</div>
</td>
<td>
<div class="data-row">
<span class="data-item">${formatDate(questionnaire.createTime)}</span>
<span class="data-item">${formatDate(questionnaire.publishTime)}</span>
<span class="data-item">${formatDate(questionnaire.data.endTime)}</span>
<span class="data-item">${questionnaire.status}</span>
<div class="data-item">
<button onclick="publishQuestionnaire('${questionnaire.id}')"
${questionnaire.status !== '未发布' ? 'disabled style="opacity: 0.5; cursor: not-allowed;"' : ''}>
发布问卷
</button>
<button onclick="editQuestionnaire('${questionnaire.id}')"
${questionnaire.status === '已发布' ? 'disabled style="opacity: 0.5; cursor: not-allowed;"' : ''}>
编辑问卷
</button>
<button onclick="deleteQuestionnaire('${questionnaire.id}')">
删除问卷
</button>
<button onclick="viewQuestionnaire('${questionnaire.id}')">
查看问卷
</button>
<button onclick="fillQuestionnaire('${questionnaire.id}')"
${questionnaire.status !== '已发布' || new Date().getTime() > new Date(questionnaire.data.endTime).getTime() ?
'disabled style="opacity: 0.5; cursor: not-allowed;"' : ''}>
填写问卷
</button>
<button onclick="analyzeQuestionnaire('${questionnaire.id}')"
${questionnaire.status !== '已发布' ? 'disabled style="opacity: 0.5; cursor: not-allowed;"' : ''}>
分析问卷
</button>
</div>
</div>
</td>
`;
tableBody.appendChild(tr);
});
}
// 删除问卷
function deleteQuestionnaire(id) {
if (!confirm('确定要删除这份问卷吗?')) return;
let questionnaireList = JSON.parse(localStorage.getItem('questionnaireList') || '[]');
questionnaireList = questionnaireList.filter(q => q.id !== id);
localStorage.setItem('questionnaireList', JSON.stringify(questionnaireList));
renderQuestionnaireList();
}
// 批量删除问卷
function batchDeleteQuestionnaires() {
const checkedBoxes = document.querySelectorAll('input[type="checkbox"]:checked');
if (checkedBoxes.length === 0) {
alert('请先选择要删除的问卷!');
return;
}
if (!confirm(`确定要删除选中的 ${checkedBoxes.length} 份问卷吗?`)) return;
let questionnaireList = JSON.parse(localStorage.getItem('questionnaireList') || '[]');
const idsToDelete = Array.from(checkedBoxes).map(cb => cb.dataset.id);
questionnaireList = questionnaireList.filter(q => !idsToDelete.includes(q.id));
localStorage.setItem('questionnaireList', JSON.stringify(questionnaireList));
renderQuestionnaireList();
}
// 全选/取消全选
function toggleSelectAll() {
const selectAllCheckbox = document.querySelector('.ctrl-all input[type="checkbox"]');
const allCheckboxes = document.querySelectorAll('table input[type="checkbox"]');
allCheckboxes.forEach(cb => cb.checked = selectAllCheckbox.checked);
}
// 发布问卷
function publishQuestionnaire(id) {
let questionnaireList = JSON.parse(localStorage.getItem('questionnaireList') || '[]');
const index = questionnaireList.findIndex(q => q.id === id);
if (index !== -1) {
const now = new Date().getTime();
questionnaireList[index].publishTime = now;
questionnaireList[index].status = '已发布';
questionnaireList[index].data.publishTime = now;
questionnaireList[index].data.status = '已发布';
localStorage.setItem('questionnaireList', JSON.stringify(questionnaireList));
renderQuestionnaireList();
}
}
// 新建问卷
function createNewQuestionnaire() {
window.location.href = 'createQuestion.html';
}
// 页面加载完成后执行
window.onload = function() {
renderQuestionnaireList();
// 添加全选事件监听
document.querySelector('.ctrl-all input[type="checkbox"]')
.addEventListener('change', toggleSelectAll);
// 添加批量删除事件监听
document.querySelector('.ctrl-all button')
.addEventListener('click', batchDeleteQuestionnaires);
// 添加新建问卷事件监听
document.querySelector('.grid-item-add')
.addEventListener('click', createNewQuestionnaire);
}
// 编辑问卷
function editQuestionnaire(id) {
window.location.href = `editQuestion.html?id=${id}`;
}
// 查看问卷
function viewQuestionnaire(id) {
window.location.href = `viewQuestion.html?id=${id}`;
}
// 填写问卷
function fillQuestionnaire(id) {
// 检查问卷状态
const questionnaireList = JSON.parse(localStorage.getItem('questionnaireList') || '[]');
const questionnaire = questionnaireList.find(q => q.id === id);
if (!questionnaire) {
alert('问卷不存在!');
return;
}
if (questionnaire.status !== '已发布') {
alert('该问卷尚未发布,无法填写!');
return;
}
// 检查问卷是否过期
const endTime = new Date(questionnaire.data.endTime).getTime();
const now = new Date().getTime();
if (now > endTime) {
alert('该问卷已过期!');
return;
}
// 跳转到填写问卷页面
window.location.href = `fillQuestion.html?id=${id}`;
}
// 分析问卷
function analyzeQuestionnaire(id) {
// 检查问卷状态
const questionnaireList = JSON.parse(localStorage.getItem('questionnaireList') || '[]');
const questionnaire = questionnaireList.find(q => q.id === id);
if (!questionnaire) {
alert('问卷不存在!');
return;
}
if (questionnaire.status !== '已发布') {
alert('未发布的问卷无法查看分析结果!');
return;
}
// 检查是否有人填写过问卷
const answers = JSON.parse(localStorage.getItem('questionnaireAnswers') || '[]');
const hasAnswers = answers.some(a => a.questionnaireId === id);
if (!hasAnswers) {
alert('暂无人填写该问卷,无法查看分析结果!');
return;
}
// 跳转到问卷分析页面
window.location.href = `analyzeQuestion.html?id=${id}`;
}
</script>
</body>
</html>