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

Survey.js矩阵单选按钮默认值设置问题求助

解决Survey.js矩阵单选按钮默认值不生效问题

要让矩阵所有行默认选中"nc(NON CONCERNE)"选项,你当前的写法不符合Survey.js的配置规则,单选矩阵的defaultValue需要以键值对对象形式配置,或者通过表达式统一设置,以下是两种可行方案:

方案1:直接配置defaultValue对象

将矩阵的defaultValue改为包含所有行对应值的对象,每个行的value作为键,默认选中的列value作为值:

questions: [
    {
        name: "ph-avant",
        type: "html",
        html: "<img class='myImg' src='/Images/ParcMachine/Gaz/PartieAvant.jpg' />",
    },
    {
        type: "panel",
        title: "CONTROLE VISUEL",
        startWithNewLine: scrW,
        questions: [
            {
                name: "Avant",
                type: "matrix",
                isRequired: true,
                isAllRowRequired: true,
                // 修改为对象格式,指定每行默认选中的列值
                defaultValue: {
                    "Fourches": "nc",
                    "Phares": "nc",
                    "Autres": "nc",
                    "Chaines": "nc"
                },
                title: "Partie Avant",
                columns: [
                    { value: "ok", text: "OK" },
                    { value: "ko", text: "NON OK" },
                    { value: "nc", text: "NON CONCERNE" }
                ],
                rows: [
                    { value: "Fourches", text: "Fourches" },
                    { value: "Phares", text: "Phare avant droit / gauche" },
                    { value: "Autres", text: "Mât / Pare brise / Essuie glace" },
                    { value: "Chaines", text: "Chaînes / Flexibles" },
                ]
            },
            {
                name: "obs_avant",
                type: "comment",
                title: "Observations",
            }
        ]
    },
]

方案2:使用defaultValueExpression统一设置

如果行数较多,推荐用表达式批量设置,无需逐个行配置:

questions: [
    {
        name: "ph-avant",
        type: "html",
        html: "<img class='myImg' src='/Images/ParcMachine/Gaz/PartieAvant.jpg' />",
    },
    {
        type: "panel",
        title: "CONTROLE VISUEL",
        startWithNewLine: scrW,
        questions: [
            {
                name: "Avant",
                type: "matrix",
                isRequired: true,
                isAllRowRequired: true,
                // 用表达式统一设置所有行默认选中"nc"
                defaultValueExpression: "'nc'",
                title: "Partie Avant",
                columns: [
                    { value: "ok", text: "OK" },
                    { value: "ko", text: "NON OK" },
                    { value: "nc", text: "NON CONCERNE" }
                ],
                rows: [
                    { value: "Fourches", text: "Fourches" },
                    { value: "Phares", text: "Phare avant droit / gauche" },
                    { value: "Autres", text: "Mât / Pare brise / Essuie glace" },
                    { value: "Chaines", text: "Chaînes / Flexibles" },
                ]
            },
            {
                name: "obs_avant",
                type: "comment",
                title: "Observations",
            }
        ]
    },
]

注意:不需要给每个rows项单独设置defaultValue,矩阵级别的配置会覆盖行级,两种方案选其一即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 09:52:35