2011-01-06 69 views
0

我需要将某些输入文本框的内容拖到数据库中。显示第一个盒子,但我使用这个JavaScript动态创建其中一些:将值添加到动态表单

var counter = 1; 
var limit = 10; 
function addInput(divName){ 
    if (counter !== limit) { 
      var newdiv = document.createElement('div'); 
      newdiv.innerHTML = " <input type='text' name='myInputs[]' size=40>"; 
      document.getElementById(divName).appendChild(newdiv); 
      counter++; 
    } 
} 

我怎么能值添加到这将为创建的,因此每个箱子是不同的形式,我可以在PHP中引用它们?

谢谢!

+0

不知道你想要什么。你所有的输入字段都可以通过$ _POST ['myInputs']`数组访问。 – 2011-01-06 20:28:04

回答

0

编辑:从头开始,我从来没有尝试过使用数组来分组输入,但它的工作原理是这样使用它。保持名称的输入,myInputs [],但参考在PHP这样的:

你的第一场和

$_POST[myInputs][1] 

第二等。

+0

感谢您的快速回复,效果很好。我沿着相同的路线尝试了一些东西,但我不熟悉Javascript语法。我将如何表示counter + 1,所以它会以2开头? – Sebastian 2011-01-06 20:29:57

0
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>//use jQuery. it makes life easy. 
      <script>!window.jQuery && document.write(unescape('%3Cscript src="js/libs/jquery-1.4.2.js"%3E%3C/script%3E'))</script> 
      <script> 
       $(document).ready(function(){//fire when the page is ready 
        $('.addFields').click(function(){//any time someone clicks on a element with the class addFields 
         if($('.myInputs').length < 10){ // if there are less than 10 input fields... 
          $($(this).attr('rel')).append("<br/><input type='text' name='myInputs[]' class='myInputs' size='40' id='myInputs"+($('.myInputs').length)+"'>"); 
          // add another input field with an ID equal to it's place in line. 
           } 
        }); 
       }); 
      </script> 

     </head> 
     <body > 
      <a rel="#addToMe" class="addFields" href="#">Add a field</a><!-- the triggering element this can be anything that can be clicked on--> 
      <div id="addToMe"><!-- the element holding our input fields, new ones get added to the back of here, note that the trigger's rel attribute is the ID of this attribute and started with a "#' ID identifier--> 
       <input type='text' name='myInputs[]' class='myInputs' size='40' id='myInputs0'> 
      </div> 

     </body>