简体   繁体   English

如何将字符串插入文本输入数组?

[英]How to insert a string into an array of text inputs?

Basically, I have a large string that i am splitting, then splitting again. 基本上,我有一个大字符串,我分裂,然后再分裂。

I then need to use the smallest split array to place its elements into text inputs on my page. 然后,我需要使用最小的split数组将其元素放入页面上的文本输入中。

This is my Javascript 这是我的Javascript

var splitquestions = vals[2].split('\n');

              //Loop to go through all current questions
              for (var i = 0; i < splitquestions.length - 1; i++) 
              {
                  //trigger a question add where a single question data can be added into
                  $( "#add" ).trigger('click');

                  //split current question into separate items
                  var s = splitquestions[i].split(',');

                  //Loop to go over all sections in a question
                  var count = 0;
                  for(var j = 0; j < s.length; j++)
                  {
                      count = count + 1;
                      var qs = document.getElementById('questions[' + j +'][' + count + ']').value;

                      qs = s[j];

                  }

              }

There will be many questions on the page, depending how many the user will like to add. 页面上会有很多问题,具体取决于用户想要添加的数量。 Each new question block will consist of a question, 3 wrong answers, and 1 correct answer. 每个新的问题块将包含一个问题,3个错误答案和1个正确答案。

The part that is going wrong is within the last loop. 出错的部分是在最后一个循环中。 This is where I need to grab each individual element within the 's' array, and place it within each text input. 这是我需要抓取's'数组中的每个单独元素,并将其放在每个文本输入中的位置。

This is how the raw data is displayed before it is split by the 'splitquestions' variable: 这是原始数据在被“splitquestions”变量拆分之前的显示方式:

question1,incorrect-answer1,incorrect-answer2,incorrect-answer3,correct-answer
question2,incorrect-answer1,incorrect-answer2,incorrect-answer3,correct-answer
question3,incorrect-answer1,incorrect-answer2,incorrect-answer3,correct-answer

As you can see from above, each question is separated by a line-break, being \\n, then each individual part is comma separated. 从上面可以看出,每个问题都以换行符分隔,为\\ n,然后每个单独的部分以逗号分隔。

Each question input has a multidimensional variable assigned to its ID. 每个问题输入都有一个分配给其ID的多维变量。 For example, using the data above, the first line of data, along with the very first element (being question1) would be question[1][1]. 例如,使用上面的数据,第一行数据和第一个元素(问题1)将是问题[1] [1]。 Another example would be 'incorrect-answer1' on the third line of data, which would be question[3][2]. 另一个例子是第三行数据的'wrong-answer1',这将是问题[3] [2]。 The first number is the question number, and the second number is the element number. 第一个数字是问题编号,第二个数字是元素编号。

I hope that I've explained this well enough, since I am a little confused on how to explain it myself since I am new to multidimensional arrays and loops inside loops. 我希望我已经很好地解释了这一点,因为我对如何自己解释它有点困惑,因为我不熟悉多维数组和循环内部的循环。 So please, if you need any additional information, just post a comment and I'll do my best. 所以,如果您需要任何其他信息,请发表评论,我会尽我所能。

If needed , this is the function that creates the question elements dynamically: 如果需要 ,这是动态创建问题元素的函数:

function dynamicForm () {

    //set a counter
    var i = $('.dynamic-input#form-step2').length + 1;
    //alert(i);
    //add input
    $('a#add').click(function () {
        $('<table><tr><td><p><span class="left"><input type="text" class="dynamic-input" name="questions[' +i +'][1]" id="' + i + '" placeholder="Question" /></span>' + '<span class="right"><input type="text" class="dynamic-input" name="questions[' +i +'][2]" id="' + i + '" placeholder="Distraction 1" /><br /><input type="text" class="dynamic-input" name="questions[' +i +'][3]" id="' + i + '" placeholder="Distraction 2" /><br /><input type="text" class="dynamic-input" name="questions[' +i +'][4]" id="' + i + '" placeholder="Distraction 3" /><br /><input type="text" class="dynamic-input" name="questions[' +i +'][5]" id="' + i + '" placeholder="Correct Answer" /><br /><a href="#">Remove</a></span></p></td></tr></table>').fadeIn("slow").appendTo('#extender');
        i++;

        $("a:contains('Remove')").click(function () {
                $(this).parent().parent().remove();
        });

        return false;
    });


    //fadeout selected item and remove
    $("#form-step2.dynamic-input").on('click', 'a', function () {
        $(this).parent().fadeOut(300, function () {
            $(this).empty();
            return false;
        });
    });
}

After further discussions with the OP, we fixed the code ended up being what's below. 在与OP进行进一步讨论后,我们修改了代码,最终得到了以下内容。 Basically, his input numbers are starting at index 1 instead of 0, so that was one of the issues. 基本上,他的输入数字从索引1而不是0开始,所以这是问题之一。 He was also trying to select by id while the inputs in question only had a name attribute. 他也试图通过id进行选择,而有问题的输入只有一个name属性。

//Loop to go over all sections in a question
for (var j = 1, len = s.length; j <= len; j++) { 
    $('input[name="questions[' + (i + 1) + '][' + j + ']"]').val(s[j - 1]); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM