You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在LimeSurvey v6中基于用户输入动态创建李克特量表矩阵?

在LimeSurvey v6中实现动态行数的机构输入与对应矩阵评估问题

核心思路

放弃预设固定行数的方案,通过JavaScript动态生成问题1的输入行,同时实时同步生成问题2的矩阵行,实现输入N个机构就自动生成N行评估矩阵的效果,无行数限制。

步骤1:实现动态机构输入(问题1)

  • 新建「Multiple short text」题型问题,命名为question1,仅保留1个默认子问题(如SQ001),删除其余预设子问题。
  • 在问题的「JavaScript」编辑框中插入以下代码,实现新增/删除输入行功能:
$(document).ready(function() {
    // 给初始行添加删除按钮
    $('#question1 .answer-item:first').append('<button type="button" class="remove-row btn btn-sm btn-danger ml-2">删除</button>');
    
    // 添加"新增机构"按钮
    $('#question1 .question-container').append('<button type="button" id="add-institution" class="btn btn-sm btn-primary mt-2">新增机构</button>');
    
    // 新增行逻辑
    $('#add-institution').click(function() {
        const lastRow = $('#question1 .answer-item:last');
        const newRow = lastRow.clone();
        // 重置输入框并更新子问题命名
        newRow.find('input').val('').attr('name', function(i, oldName) {
            const num = parseInt(oldName.match(/SQ(\d+)/)[1]) + 1;
            return oldName.replace(/SQ\d+/, 'SQ' + String(num).padStart(3, '0'));
        });
        newRow.append('<button type="button" class="remove-row btn btn-sm btn-danger ml-2">删除</button>');
        lastRow.after(newRow);
        // 同步更新矩阵
        updateMatrix();
    });
    
    // 删除行逻辑(支持动态生成的按钮)
    $('#question1').on('click', '.remove-row', function() {
        const rowCount = $('#question1 .answer-item').length;
        if (rowCount > 1) { // 至少保留一行
            $(this).closest('.answer-item').remove();
            updateMatrix();
        }
    });
    
    // 输入内容变化时同步更新矩阵
    $('#question1 input').on('input', updateMatrix);
});

步骤2:实现动态矩阵评估(问题2)

  • 新建「Array (Numbers)」题型问题,命名为question2,删除所有预设子问题,保留3列,分别设置列标题为「重要性(1-7分)」「影响力(1-7分)」「当前协作情况(1-7分)」,分值范围设为1-7。
  • 在问题的「JavaScript」编辑框中插入以下代码,实现矩阵行的动态生成:
function updateMatrix() {
    const $matrixTable = $('#question2 table.subquestion-list');
    const $matrixBody = $matrixTable.find('tbody');
    $matrixBody.empty();
    
    // 遍历问题1的所有输入框
    $('#question1 input').each(function(index) {
        const institutionName = $(this).val().trim();
        const rowIndex = index + 1;
        const subQName = 'SQ' + String(rowIndex).padStart(3, '0');
        
        // 构建矩阵行HTML
        let rowHtml = '<tr>';
        // 机构名称列
        rowHtml += `<td class="text-left subquestion">${institutionName || '未命名机构'}</td>`;
        // 三个评分列
        for (let col = 1; col <= 3; col++) {
            const inputName = `question2[${subQName}][${col}]`;
            rowHtml += `<td class="answer-item"><input type="text" name="${inputName}" class="form-control" maxlength="1" pattern="[1-7]" placeholder="1-7"></td>`;
        }
        rowHtml += '</tr>';
        
        $matrixBody.append(rowHtml);
    });
    
    // 无输入时显示提示
    if ($('#question1 input').filter(function() { return $(this).val().trim() !== ''; }).length === 0) {
        $matrixBody.append('<tr><td colspan="4" class="text-center text-muted">请先添加合作机构</td></tr>');
    }
}

$(document).ready(function() {
    // 页面加载时初始化矩阵
    updateMatrix();
});

关键注意事项

  • 两个问题必须放在同一问卷页面,否则无法实时同步数据。
  • 评分输入框通过pattern="[1-7]"限制输入范围,也可结合LimeSurvey内置验证增强校验。
  • 删除行逻辑保留至少一行输入框,避免空输入导致的逻辑异常。
  • 动态生成的子问题名称符合LimeSurvey命名规则,确保数据能正常存储。

内容的提问来源于stack exchange,提问作者Tiago

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 17:28:22