Php 向动态窗体添加值
我需要将一些输入文本框的内容拉入数据库。将显示第一个框,但我正在使用此Javascript动态创建其中一些框:Php 向动态窗体添加值,php,javascript,html,Php,Javascript,Html,我需要将一些输入文本框的内容拉入数据库。将显示第一个框,但我正在使用此Javascript动态创建其中一些框: var counter = 1; var limit = 10; function addInput(divName){ if (counter !== limit) { var newdiv = document.createElement('div'); newdiv.innerHTML = " <input type='t
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++;
}
}
var计数器=1;
var限值=10;
函数addInput(divName){
如果(计数器!==限制){
var newdiv=document.createElement('div');
newdiv.innerHTML=“”;
document.getElementById(divName).appendChild(newdiv);
计数器++;
}
}
如何向表单中添加一个值,该值对于创建的每个框都不同,以便在PHP中引用它们
谢谢 编辑:请注意,我从未尝试过使用数组对输入进行分组,但它很有效,所以请改用它。将输入的名称保留为myInputs[],但在php中的引用如下:
$_POST[myInputs][0]
为您的第一个领域和
$_POST[myInputs][1]
对于第二个etc..//使用jQuery。它使生活变得轻松。
<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>
!window.jQuery&&document.write(unescape(“%3Cscript src=”js/libs/jQuery-1.4.2.js”%3E%3C/script%3E'))
$(document).ready(函数(){//在页面准备就绪时激发
$('.addFields')。每当有人单击类为addFields的元素时,单击(function(){//)
如果($('.myInputs').length<10){//如果输入字段少于10个。。。
$($(this.attr('rel')).append(
);
//添加另一个ID等于其在行中位置的输入字段。
}
});
});
不确定您想要什么。您的所有输入字段都可以通过$\u POST['myInputs']
数组访问。感谢您的快速回复,效果很好。我尝试了同样的方法,但我不熟悉Javascript语法。我如何表示计数器+1,使其以2开头?为什么要以2开头?因为第一个框已经存在,javascript为添加的框提供服务。因此,添加的第一个将是t2.ah,在这种情况下,只要所有输入都具有属性name='myInputs[],上述操作仍然有效