2011-04-30 79 views
0

我怎麼能動態地創建元素的值,而該元素的量之前 由於基於輸入文本的新元素..創建不亞於輸入

+1

是否希望創建一個元素數組,其數組中的元素數量與某些輸入值一樣大? – Bart 2011-04-30 11:34:53

+0

讀取值並在循環中創建元素。你試過什麼了? – 2011-04-30 11:38:07

+0

right, and the element is combo box – 2011-04-30 11:38:15

回答

0

Finnaly我發現它

JS:

$(function() { 
    $("#amount").keyup(function() { 
     var amount = $("#amount").val(); 
     var i; 
     if(amount != ""){ 
      for(i=1;i<amount;i++){ 
       var c = $('#cloned:first').clone(true); 
       c.attr("class","cloned"); 
       $('#cloned:last').after(c); 
      } 
      $('#tr_table').show("slow"); 
      $("#hidden").val($("#amount").val()); 
     }else if(amount == ""){ 
      var j = $("#hidden").val(); 
      for(var k=1;k<j;k++){ 
       $("#cloned").remove(); 
      } 
      $("#tr_table").hide("slow"); 
     } 

    }) 
}) 

HTML:

<table> 
<tr> 
    <td> 
     <input type="text" id="amount"> 
     <input type="hidden" id="hidden"> 
    </td> 
</tr> 
<tr id="tr_table" style="display: none;"> 
    <td> 
     <table id="cloned"> 
      <tr> 
       <td>Some Label</td> 
       <td>:</td> 
       <td> 
        <input type="text" value="SOME INPUT" /> 
       </td> 
      </tr> 
     </table> 
    </td> 
</tr> 

0
<input type="text" id="txt"> 
<a id="elm">create elemrnt</a> 

<div id="foo"> 
</div> 





    $(document).ready(function(){ 
    $("#elm").click(function(){ 

var a = parseInt($("#txt").val()); 

var i=0; 
for (i=1;i<=a;i++) 
{ 
    $("#foo").append("<div>hello world</div>"); 
    } 

}); 

});