admin管理员组文章数量:1289541
I am trying to make a script that, when I choose in the dropdown 4, the div is repeated 4 times.
<div>repeat<div>
<script type = "text/javascript">
document.write('<select>')
for (var i = 0; i < 10; i++) {
document.write('<option value=' + i + '>' + i + '</option>');
}
document.write('</select>')
</script>
Now the problem is repeat the number of times that the user choose in dropdown.
demo
I am trying to make a script that, when I choose in the dropdown 4, the div is repeated 4 times.
<div>repeat<div>
<script type = "text/javascript">
document.write('<select>')
for (var i = 0; i < 10; i++) {
document.write('<option value=' + i + '>' + i + '</option>');
}
document.write('</select>')
</script>
Now the problem is repeat the number of times that the user choose in dropdown.
demo
Share Improve this question edited Jun 10, 2011 at 16:41 Vivin Paliath 95.6k42 gold badges230 silver badges301 bronze badges asked Jun 10, 2011 at 15:58 daniel__daniel__ 11.8k16 gold badges67 silver badges91 bronze badges 2- Repeat what? You want to let the user select the numbers in the dropdown? – Luceos Commented Jun 10, 2011 at 16:00
- @clamp, I believe it is, he does have this question tagged with jquery – matchew Commented Jun 10, 2011 at 16:10
4 Answers
Reset to default 4First, don't use document.write
.
You've tagged your question with jQuery and so I'm assuming that you're using that.
Something like this should work (fiddle):
HTML:
<div id="repeatDiv">repeat</div>
<select id = "mySelect">
<option value = "1">1</option>
<option value = "2">2</option>
<option value = "3">3</option>
...
<option value = "10">10</option>
</select>
Javascript:
jQuery("#mySelect").change(function() {
var value = jQuery(this).val();
var $repeatDiv = jQuery("#repeatDiv");
for(var i = 0; i < value; i++) {
$repeatDiv.after($repeatDiv.clone().attr("id", "repeatDiv" + new Date().getTime()));
}
});
I modified my solution to use a clone and give each element unique id's.
The non-jQuery version is a little more verbose (fiddle):
document.getElementById("mySelect").addEventListener("change", function(event) {
var value = event.target.options[event.target.selectedIndex].value;
var repeatDiv = document.getElementById("repeatDiv");
for(var i = 0; i < value; i++) {
var newDiv = document.createElement("div");
newDiv.id = "repeat" + new Date().getTime();
newDiv.innerHTML = repeatDiv.innerHTML;
repeatDiv.parentNode.insertBefore(newDiv, repeatDiv.nextSibling); //essentially an insertAfter
}
}, true);
Listening to the "onchange" event on the select element will give you the feedback you need to modify the document structure. Fiddle example
HTML
<div id="repeat"></div>
JavaScript
var select_element = document.createElement("select");
var option_element;
for (var i = 0; i < 10; i++) {
option_element = document.createElement("option");
option_element.value = i;
option_element.text = i;
select_element.appendChild(option_element);
}
select_element.addEventListener("change", function (event) {
var repeat_container = document.getElementById("repeat");
var div_element;
// Remove previously added divs
while (repeat_container.firstChild) {
repeat_container.removeChild(repeat_container.firstChild);
}
// Add new divs
for (var i = 0; i < event.currentTarget.value; i++) {
div_element = document.createElement("div");
div_element.style.backgroundColor = "rgb(0,255," + (i * 20) + ")";
div_element.style.height = "10px";
repeat_container.appendChild(div_element);
}
}, false);
document.body.appendChild(select_element);
here is a neat demonstration:
JSFiddle demo
And here: the code used:
//NUMERATE SELECT
for (var i = 0; i < 10; i++) { // add counts in a for loop
$('.select select').append('<option value=' + i + '>' + i + '</option>');
}
//#
//DUPLICATE PLUGIN
$.fn.duplicate = function(count, cloneEvents) {
var tmp = [];
for (var i = 0; i < count; i++) {
$.merge(tmp, this.clone(cloneEvents).get());
}
return this.pushStack(tmp);
};
//SELECT CHANGE FUNCTION (on change get value and clone)
$('.select select').change(function(){ // on change...
var numOfClones = $(this).val(); // get value...
$('#holder').html(''); // empty holder if there are some old clones
$('.repeat').duplicate(numOfClones).addClass('new').appendTo('#holder'); // duplicate; fill holder with new clones;
});
A simple solution if you can't use jQuery:
HTML:
<select id="repeater" onChange="updateDivs(this);">
<option value="1"> 1 </option>
<option value="2"> 2 </option>
<option value="3"> 3 </option>
<option value="4"> 4 </option>
</select>
<div id="holder">
</div>
Javascript:
function updateDivs(select){
times=select.value;
holder=document.getElementById("holder");
while(holder.hasChildNodes()){
holder.removeChild(holder.firstChild);
}
for(i=1; i<=times; i++){
div = document.createElement("div");
div.appendChild(document.createTextNode("Repeat" + i));
holder.appendChild(div);
}
}
Document.write is a bad idea, you should try to modify dom tree using appendChild, removeChild etc. instead
版权声明:本文标题:javascript - How can I replicate a div a certain number of times based on a select-box value? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741405841a2376939.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论