【问题标题】:How to change variables within SurveyJS JSON by altering global variables如何通过更改全局变量来更改 SurveyJS JSON 中的变量
【发布时间】:2021-01-20 18:23:35
【问题描述】:

我编写了一个应用程序,旨在生成一个调查,该调查会在调查的每次迭代中发生变化。我有一些随着每次迭代而变化的全局变量,我想将它们包含在调查结果中。我无法在 JSON 中更新变量。我尝试将整个系统包装在 start 函数中,但这似乎破坏了整个调查。我不确定在这里做什么 - 我知道这与我的变量和函数如何相互嵌套有关,但我似乎无法找出理想的安排。我希望能够在最终 JSON 中包含更改变量,以便我可以将其存储并在我的 SurveyJS 服务存储中查看它。有什么建议吗?

整个应用程序的小提琴 - https://jsfiddle.net/cormundo/n9hjrpzx/1/

最相关的代码:

此启动功能隐藏登录模式并在单击登录按钮时显示调查:

function start() {
    username = document.getElementById("user_PID").value;
    alert("your PID is " + username);
    while (count > 20);
        modal.style.display = "none";
        document.getElementById("surveyElement").style="display:inline-block;width:100%;";
        document.getElementById("streetscape").style="display:visible";
        document.getElementById("btn1").style="display:none";
  };

脚本的其余部分运行调查,最重要的方面是嵌套在 JSON 中的变量 -

var json;

function jsoner (){
        knxer();               <- increments global count
        keynamer();            <- attatches keyname to survey image from external site
        mapilinker();          <- More external site business
        imgur();               <- See above(To be combined with other functions here) 
        usernamer();           <- Theoretically attaches global username, not working
        console.log (username)

    json = {
 pages: [
  {
   name: "page1",
   elements: [
    {
     type: "text",
     name: "UID",
     visible: false,
     title: "USERID",
     defaultValue: (PID2)   
    },
    {
     type: "text",
     name: "Keyn",
     visible: false,
     title: "Key",
     defaultValue: (keyN2)
    },
    {
     type: "text",
     name: "CNT",
     visible: false,
     title: "TIME",
     defaultValue: (count)
    }...

最后,需要注意的是,每次提交调查时,页面都应该更新为具有不同 (PID2) (keyN2) 和 (count) 变量的新版本调查,以及新图像供用户回答有关问题。涵盖所有内容的底部 -

function update(){
      knxer();
      keynamer();
      mapilinker();
      imgur();
      usernamer();
      jsoner();
}



jsoner();



     window.survey = new Survey.Model(json);

         survey
             .onComplete
             .add(function (result) {
                 var PID = username
                 var results = PID + ":\n" + JSON.stringify(result.data, null, 3);
                 document
                     .querySelector('#surveyResult')
                     .textContent = results;
             window.count ++;
             update();
             console.log(knx);
             console.log(count);
             console.log(keyname);
             console.log(mapilink);
             console.log(username);
             survey.clear();
             survey.render();
             });

         $("#surveyElement").Survey({model: survey});

为混乱道歉。谢谢!

【问题讨论】:

    标签: javascript json surveyjs


    【解决方案1】:

    问题是您在收集用户名之前声明了您的调查。因此,您的调查使用包含 UID 问题默认值的 null 的 JSON 数据进行实例化。 你可以通过做两件事来解决它:

    1. 将以下代码部分移动到 jsoner() 函数中:
    window.survey = new Survey.Model(json);
    
     survey
       .onComplete
       .add(function(result) {
         var PID = username
         var results = PID + ":\n" + JSON.stringify(result.data, null, 3);
         document
           .querySelector('#surveyResult')
           .textContent = results;
         window.count++;
         update();
         console.log(knx);
         console.log(count);
         console.log(keyname);
         console.log(mapilink);
         console.log(username);
         survey.clear();
         survey.render();
       });
    
     $("#surveyElement").Survey({
       model: survey
     });
    
    1. 将调用移至 jsoner() 函数内的 start() 函数

    我叉了你的小提琴,让它在这里工作:https://jsfiddle.net/7u8jnpry/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多