具有多个答案选项的Javascript动态数组窗口

时间:2015-04-01 15:42:05

标签: javascript html css

我正在处理多个阵列并遇到一个问题。 当我只生成一个问题div时,我可以毫无问题地添加其他问题,但如果我添加另一个问题div,我就不能向两个窗口添加其他问题(同样适用于任何其他数字),错误哪个我得到的是newdiv [counterq]是未定义的。有人可以帮我解决这个问题吗?谢谢!

另外,如何在下面创建新的一个选项输入下移动div AddOption?

如果不能以正确的方式解释,我很擅长编程抱歉。谢谢!

编辑:更新了新问题。没有想创造单独的主题。

HTML:



var counterq = 0;
var limitq = 3;
var countero = 0;
var limito = 5;

function AddContent(divName) {
    countero = 0;
  if (counterq == limitq) {
    alert("You have reached the limit of adding " + counterq + " inputs");
  } else {
    var newdiv = new Array()
    newdiv[counterq] = document.createElement('div');
    newdiv[counterq].className = "ContentWindow[" + counterq + "]";   
    newdiv[counterq].innerHTML = "<p class='ContentQuestion'>Question " + (counterq + 1) + " </p> <input type='text' class='AddQuestionInput' value='Type your question' name='myQuestion[" + counterq + "]'>"; 
    if (countero == limito) {
      alert("You have reached the limit of adding " + countero + " options");
    } else {
        newdiv[counterq].innerHTML += "<div class='OptionInputOuterWindow'><span class='OptionInputDescription'>Option " + (countero + 1) + "</span> <input type='text' class='AddOptionInput' name='myQuestion[" + counterq + "]"+"[myInputs]"+"[" + countero + "]'></div>";
        newdiv[counterq].innerHTML += "<div class='OptionInputOuterWindow'><span class='OptionInputDescription'>Option " + (countero + 2) + "</span> <input type='text' class='AddOptionInput' name='myQuestion[" + counterq + "]"+"[myInputs]"+"[" + (countero+1) + "]'></div>";
        document.getElementById(divName).appendChild(newdiv[counterq]);
        countero += 2;
         AddOption = function() {
            var counterq = 0;
            var limito = 5;
            if (countero == limito) {
                alert("You have reached the limit of adding " + countero + " options");
            } else {
                newdiv[counterq].innerHTML += "<div class='OptionInputOuterWindow'><span class='OptionInputDescription'>Option " + (countero + 1) + "</span> <input type='text' class='AddOptionInput' name='myQuestion[" + counterq + "]"+"[myInputs]"+"[" + countero + "]'></div>";
                $("div[class*=ContentWindow]").css("height", "+=27");
                countero++;
            }
        };
    }
        $(".container").css("height", "+=344");
        newdiv[counterq].innerHTML += "<div class='AddOption' onclick='AddOption();'><img src='/img/Plussmall.png'>Add option</div>";
    counterq++;
  }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <div id="Content"></div>


        <div class="AddContent" onclick="AddContent('Content');" >
        	<img src="/img/Plussmall.png">Add content
        </div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

如果您希望计数器增加2,请使用countero += 2代替countero++

var counterq = 0;
var limitq = 3;
var countero = 0;
var limito = 5;

function AddContent(divName) {
  if (counterq == limitq) {
    alert("You have reached the limit of adding " + counterq + " inputs");
  } else {
    var newdiv = new Array()
    newdiv[counterq] = document.createElement('div');
    newdiv[counterq].className = 'new-rect';
    if (counterq == limito) {
      alert("You have reached the limit of adding " + countero + " options");
    } else {
      newdiv[counterq].innerHTML = "Entry " + (countero + 1) + " <br><input type='text' name='myInputs[" + countero + "]'><br>Entry " + (countero + 2) + " <br><input type='text' name='myInputs[" + countero + "]'><br>";
      document.getElementById(divName).appendChild(newdiv[counterq]);
      countero += 2;
    }

    counterq++;
  }
}
.new-rect {
  width: 300px;
  height: 100px;
  border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="Content"></div>

<input type="button" class="AddContent" value="Add content" onclick="AddContent('Content');">