根据javascript中给定的输入数字动态显示div的数量
我希望div根据输入中获得的数字显示。如果输入为3;我想显示3个maindiv块。如果它变为1;只应显示1个maindiv块。有人提出解决方案吗根据javascript中给定的输入数字动态显示div的数量,javascript,html,Javascript,Html,我希望div根据输入中获得的数字显示。如果输入为3;我想显示3个maindiv块。如果它变为1;只应显示1个maindiv块。有人提出解决方案吗 函数showTrips(){ var参数=document.getElementById(“行程次数”); var value=argument.options[argument.selectedIndex].text; var main=document.getElementById(“mainform”); var mains=document.
函数showTrips(){
var参数=document.getElementById(“行程次数”);
var value=argument.options[argument.selectedIndex].text;
var main=document.getElementById(“mainform”);
var mains=document.getElementById(“maindiv”);
var newdiv=document.createElement('div');
newdiv.innerHTML=mains.innerHTML;
对于(变量i=1;i
出行次数:
1.
2.
3.
4.
5.
行程详情
您的id“行程次数”不是有效的id,不应包含任何空格
如果它包含
<select id="Numberoftrips">
<option value="1">1</option>
<option value="2" >2</option>
<option value="3"selected="selected">3</option>
</select>
1.
2.
3.
使用jquery,您可以尝试
$(function(){
$('#Numberoftrips').change(function(){
var argument = document.getElementById("Numberoftrips");
var value = argument.options[argument.selectedIndex].value;
var main = document.getElementById("mainform");
var mains = document.getElementById("maindiv");
$( "#maindiv" ).empty();
for (var i = 1; i <= value; i++)
{
var div = document.createElement('div');
maindiv.appendChild(div);
div.className = 'aClassName';
div.innerHTML = '<span class="">Hello world.</span>';
}});
});
$(函数(){
$('#Numberoftrips')。更改(函数(){
var参数=document.getElementById(“Numberoftrips”);
var value=argument.options[argument.selectedIndex].value;
var main=document.getElementById(“mainform”);
var mains=document.getElementById(“maindiv”);
$(“#maindiv”).empty();
对于(var i=1;i请尝试以下代码。
[更新]
函数showTrips(){
var参数=document.getElementById(“Numberoftrips”);
var value=argument.options[argument.selectedIndex].text;
var ni=document.getElementById(“容器”);
var mains=document.getElementsByClassName(“行”);
var innerHt=mains[0].innerHTML;
var newdiv=document.createElement('div');
var divs=[];
而(我是第一个孩子){
非独生子女(非长子);
}
对于(变量i=1;i
出行次数:
1.
2.
3.
4.
5.
行程详情
请用完整的代码修改它。对于此类问题,您应该添加相关的HTML和CSS。值得注意的是,HTML中的id
不能包含空格。您将多次附加相同的newdiv
,而不是该节点的新副本。请明确您的问题。何时调用that javascript函数。一旦更改选择列表,你想每次插入新的div还是隐藏的现有div???我尝试了这个方法,但不起作用。它实际上附加了多个div,而不是所需的确切数字。好吧。我明白了你的意思。但是当你想调用showTrips函数时?我想在e select list.好的..那很好..现在你想每次插入一个新的div还是想根据所选的选项显示一组隐藏的div.然后我想@Rasel已经用一些jQuery回答了你的问题…你想用纯javascript吗?+1来使用jQuery.但是我想他想使用javascript.如果你将代码更新为然后它可能会满足他的要求。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>