另:用js动态给from增加input
http://www.sieye.cn/showArticle.asp?nameID=2016122914103311
自(参考):http://m.blog.csdn.net/article/details?id=50084687
原文代码:
<html>
<head>
<title>动态创建按钮</title>
<script src='http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js'></script>
</head>
<body>
<a href="#" id="AddMoreFileBox" class="btn btn-info">添加更多的input输入框</a></span></p>
<div id="InputsWrapper">
<div><input type="text" name="mytext[]" id="field_1" value="Text 1"/><a href="#" class="removeclass"><input type='button' value='删除'></a></div>
</div>
<script>
$(document).ready(function() {
var MaxInputs = 8; //maximum input boxes allowed
var InputsWrapper = $("#InputsWrapper"); //Input boxes wrapper ID
var AddButton = $("#AddMoreFileBox"); //Add button ID
var x = InputsWrapper.length; //initlal text box count
var FieldCount=1; //to keep track of text box added
$(AddButton).click(function (e) //on add input button click
{
if(x <= MaxInputs) //max input box allowed
{
FieldCount++; //text box added increment
//add input box
$(InputsWrapper).append('<div><input type="text" name="mytext[]" id="field_'+ FieldCount +'" value="Text '+ FieldCount +'"/><a href="#" class="removeclass"><input type="button" value="删除"></a></div>');
x++; //text box increment
}
return false;
});
$("body").on("click",".removeclass", function(e){ //user click on remove text
if( x > 1 ) {
$(this).parent('div').remove(); //remove text box
x--; //decrement textbox
}
return false;
})
});
</script>
</body>
</html>
修改:
<html>
<head>
<title>动态创建按钮</title>
<script src='../mkJs/jquery-1.11.1.min.js'></script>
</head>
<body>
<a href="#" id="AddMoreFileBox" class="btn btn-info">添加更多的input输入框</a></span></p>
<a href="#" id="DelMoreFileBox" class="btn btn-info">删除更多的input输入框</a></span></p>
<div id="InputsWrapper">
<input type="text" name="mytext[]" id="field_1" value="Text 1"/>
</div>
<script>
$(document).ready(function() {
var MaxInputs = 8; //maximum input boxes allowed
var InputsWrapper = $("#InputsWrapper"); //Input boxes wrapper ID
var AddButton = $("#AddMoreFileBox"); //Add button ID
var DelButton = $("#DelMoreFileBox"); //Add button ID
var x = InputsWrapper.length; //initlal text box count
var FieldCount=1; //to keep track of text box added
$(AddButton).click(function (e) //on add input button click
{
if(x <= MaxInputs) //max input box allowed
{
FieldCount++; //text box added increment
//add input box
$(InputsWrapper).append('<input type="text" name="mytext[]" id="field_'+ FieldCount +'" value="Text '+ FieldCount +'"/>');
x++; //text box increment
}
return false;
});
$(DelButton).click(function (e) //on add input button click
{
$(InputsWrapper).children("input").eq(0).remove();
x--;
return false;
});
});
</script>
</body>
</html>