基本上,我有一個大的字符串,我分裂,然後再次分裂。如何將字符串插入到文本輸入數組中?
然後,我需要使用最小的拆分數組將其元素放置到我的頁面上的文本輸入中。
這是我的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];
}
}
會有頁面上的許多問題,這取決於用戶會喜歡多少補充。每個新的問題塊將包含一個問題,3個錯誤答案和1個正確答案。
出錯的部分在最後一個循環內。這是我需要抓住's'數組中的每個單獨元素,並將其放在每個文本輸入中的地方。
這是如何之前它由「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
正如從上面看到的,每個問題由一個斷行分離,是\ n ,然後每個單獨的部分用逗號分隔。
每個問題輸入都有一個分配給其ID的多維變量。例如,使用上面的數據,第一行數據以及第一個元素(即question1)將成爲問題[1] [1]。另一個例子是第三行數據中的「錯誤答案1」,這將成爲問題[3] [2]。第一個數字是問題編號,第二個數字是元素編號。
我希望我已經解釋得很好,因爲我對如何解釋它自己有點困惑,因爲我是多維數組和循環內循環的新手。所以請,如果您需要任何其他信息,只需發表評論,我會盡我所能。
如果需要,這是動態創建的問題要素的功能:
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;
});
});
}
u能模擬同在的jsfiddle,HTTP://jsfiddle.net – dreamweiver
@dreamweiver - 我認爲這是遠遠困難的,因爲有一些涉及 – Fizzix
我猜你可以包括許多其他功能相關的功能,它定義了你的概率 – dreamweiver