我希望用户能够创建不确定数量的问题。一旦用户创建了一个问题,我想要出现另一个问题(问题2),依此类推......
我该怎么做?
由于
答案 0 :(得分:0)
我会做类似的事情:
function addClone() {
var cloned = document.getElementById('test').cloneNode(true); //clone the element
document.body.appendChild(cloned); //add the cloned element to the page
}
<div id='test'>iadwjoam</div>
<input type="checkbox" id="xxx" name="xxx" onchange="addClone()" />
cloneNode()
- https://developer.mozilla.org/en-US/docs/Web/API/Node.cloneNode
答案 1 :(得分:0)
你可以尝试这样的事情
HTML
<div class="input-container">
<input class="question" placeholder="Question" type="text"/>
</div>
<div class="add-option">+</div>
JS
//click the '+' sign
$(".add-option").click(function(){
//find the first input and clone it then append it to the container
$(".input-container input:first").clone().appendTo(".input-container");
//clear any value from the next input had there been any added
$(".input-container input:last").val("");
});
<强>更新强>
我遗漏了关于复选框的部分,从UI的角度来看,我认为你不需要一个复选框来完成这个,不确定它是否表明“这已经完成并且我想继续”但是这取决于你决定。这是一个新的小提琴
HTML
<div class="input-container">
<input class="question" placeholder="Question" type="text"/>
</div>
<div class="add-option"><input type="checkbox"/></div>
JS
//click the checkbox
$(".add-option input[type=checkbox]").click(function(){
//find the first input and clone it then append it to the container
$(".input-container input:first").clone().appendTo(".input-container");
//clear any value from the next input had there been any added
$(".input-container input:last").val("");
//reset the checkbox
$(this).prop("checked", false);
});
答案 2 :(得分:0)
在javascript中你可以这样做
var i = 0;
function addMore() {
i++;
var parentDiv = document.createElement("div");
var childLabel = document.createElement("label");
childLabel.appendChild(document.createTextNode("Enter Value " + i));
var childTextBox = document.createElement("input");
childTextBox.type = "text";
childTextBox.id = "txtInput" + i;
var childCheckBox = document.createElement("input");
childCheckBox.type = "checkbox";
childCheckBox.onchange = addMore;
parentDiv.appendChild(childLabel);
parentDiv.appendChild(childTextBox);
parentDiv.appendChild(childCheckBox);
document.body.appendChild(parentDiv);
}
<div>
Add Items
<input type="checkbox" onchange="addMore()">
</div>
希望这个帮助
答案 3 :(得分:0)
这里或多或少会使用纯JavaScript做什么(请参阅说明评论)
// Create a copy of a blank Question node
question_template = document.getElementById("questions").firstElementChild.cloneNode(true);
// A nice cloning wrapper
new_question = function(){return question_template.cloneNode(true);};
// Something to do when the user is finished putting in questions
alrt = function(){
alert(Array.prototype.map.call(document.getElementById(
"questions").getElementsByTagName("input"),
function(elt){return elt.value;}));
};
// Hook for when text is placed in textbox
addnew = function(){
// Remove the event listener (If you don't do this, then on every
// keystroke a new box will be created
document.getElementById("questions").lastElementChild.oninput = function(event){};
// Add a new line
document.getElementById("questions").appendChild(document.createElement("br"));
// Add a new question box
document.getElementById("questions").appendChild(new_question());
};
&#13;
<div id="questions">
<input type="text" oninput="addnew()"/>
</div>
<input type="button" value="Done" onclick="alrt()"/>
&#13;
更新:我刚刚在主要帖子的评论部分看到了您的详细说明。您可以使用以下内容实现:
// Create a copy of a blank Question node
question_template = document.getElementById("questions").firstElementChild.cloneNode(true);
// A nice cloning wrapper
new_question = function(){return question_template.cloneNode(true);};
// Something to do when the user is finished putting in questions
alrt = function(){
alert(Array.prototype.map.call(document.getElementById(
"questions").getElementsByTagName("input"),
function(elt){return elt.value;}));
};
// Hook for when text is placed in textbox
addnew = function(){
// Add a new line
document.getElementById("questions").appendChild(document.createElement("br"));
// Add a new question box
document.getElementById("questions").appendChild(new_question());
};
&#13;
<div id="questions">
<input type="text"/>
</div>
<input type="button" value="Add Another" onclick="addnew()"/>
<input type="button" value="Done" onclick="alrt()"/>
&#13;
这可以通过具有正确事件的大多数input
元素来实现。如果您需要,W3学校有a nice reference。