如何对齐动态输入字段?

时间:2014-10-20 17:31:54

标签: javascript html css

我正在创建输入字段,我希望每次for循环开始时它们都对齐左边。我怎么能实现这一目标?

HTML

<form name="myform1">
    <select id="polja2"  onchange="Gen()">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
</form>

<form name="myform">
    <select id="polja"  onchange="Gen()">
        <option ></option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>   
    </select>     
</form>

<form>   
    <div id="t">        
    </div>
    <input type="button" value="Unesite vrijednosti" onclick="korekcija(this.form)">
</form>

的JavaScript

function Gen()
    {     
    var val = document.getElementById("polja");
    val=(val.options[val.selectedIndex].value);     
    var valGrup = document.getElementById("polja2");
    valGrup=(valGrup.options[valGrup.selectedIndex].value);
    var i;
    var j;
    var e = document.getElementById("t");
    var randomColor;
        for(i=0;i<valGrup;i++) {
            randomColor="#"+(Math.random()*0xFFFFFF<<0).toString(16);
            generate(e, i,randomColor,j,val);    
        }

    }

function generate(e, i,randomColor,j,val) { 
    e.innerHTML += "Grupa: "+i+"<br>";
    e.innerHTML += "<input type='color' name ='color1' value='"+randomColor+"' id='color"+i+"'/>"+"<br>" ;
    e.innerHTML += "<input type='text'  id='naziv" +i+"'/>"+"<br>";         

    for(j=0;j<val;j++) {
        e.innerHTML += "<input type='number' id='inputpolja"+i+j+"'/>"+"<br>";
    }
}

我尝试在div标签中向左添加对齐,但它不起作用。

1 个答案:

答案 0 :(得分:0)

试试这个应该没问题我只是添加了几个位,包括css。

<style>
    .inline{
        display:inline-block;
        float:left;
    }

    .clear{
        clear:both;
    }
</style>

<form name="myform1">
    <select id="polja2"  onchange="Gen()">
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
</form>

<form name="myform">
    <select id="polja"  onchange="Gen()">
        <option ></option>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>   
    </select>     
</form>

<form>   
    <div id="t">        
    </div>
    <div class="clear"></div>
    <input type="button" value="Unesite vrijednosti" onclick="korekcija(this.form)">
</form>
    <script>
        function Gen()
    {     
    var val = document.getElementById("polja");
    val=(val.options[val.selectedIndex].value);     
    var valGrup = document.getElementById("polja2");
    valGrup=(valGrup.options[valGrup.selectedIndex].value);
    var i;
    var j;
    var e = document.getElementById("t");
    var randomColor;
        for(i=0;i<valGrup;i++) {
            randomColor="#"+(Math.random()*0xFFFFFF<<0).toString(16);
            generate(e, i,randomColor,j,val);    
        }

    }

function generate(e, i,randomColor,j,val) { 
    e.innerHTML += "<div class='clear'></div>";
    e.innerHTML += "Grupa: "+i+ "<br>";
    e.innerHTML += "<input type='color' class='inline' name ='color1' value='"+randomColor+"' id='color"+i+"'/>";
    e.innerHTML += "<input type='text' class='inline' id='naziv" +i+"'/>";         

    for(j=0;j<val;j++) {
        if(j >= val){
            e.innerHTML += "<div class='clear'></div>"; 
        } else  {     
            e.innerHTML += "<input type='number' class'inline' id='inputpolja"+i+j+"'/>";
        }
    }       
}
</script>

以上是

修改后代码的全屏JSFIDDLE