记录一下一些方法什么的。这个代码是 可以直接写成html代码后通过 js 来实现删除html的元素。这样结合php的话来实现批量删除什么的 就容易得多了。点击添加后就会增加一个文本框 这样就好使用多了。下面就是 html结合php的代码了
先看个最简单的
这个是网上找到的发出来 大家可以用用
<html>
<script type="text/javascript">
function add(){
var div = document.getElementById("add");
var input = document.createElement("input")
input.setAttribute("type","text")
input.setAttribute("name","tpiao[]")
var del = document.createElement("input")
del.setAttribute("type","button")
del.setAttribute("value","删除")
var br = document.createElement("br");
div.appendChild(input)
div.appendChild(del)
div.appendChild(br)
del.onclick = function(){
div.removeChild(input)
div.removeChild(del)
div.removeChild(br)
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<body>
<form action="?a=1" method="post" >
<input type="button" value="添加" onClick="add()" />
<input type="submit" value="提交" />
<br />
<div id="add"></div>
</form>
</body>
</html>
例子
增加或删除input然后提交给php
<html>
<script type="text/javascript">
function del(id){
var box1 = document.getElementById("neir"+id);
box1.parentNode.removeChild(box1);
var box2 = document.getElementById("del"+id);
box2.parentNode.removeChild(box2);
var box3 = document.getElementById("br"+id);
box3.parentNode.removeChild(box3);
}
function add(){
var idt = document.getElementById('idtpiao').value=parseInt(document.getElementById('idtpiao').value)+1;
var div = document.getElementById("add");
var input = document.createElement("input")
input.setAttribute("type","text")
input.setAttribute("name","tpiao[]")
input.setAttribute("value",idt)
var del = document.createElement("input")
del.setAttribute("type","button")
del.setAttribute("value","删除")
var br = document.createElement("br");
div.appendChild(input)
div.appendChild(del)
div.appendChild(br)
del.onclick = function(){
div.removeChild(input)
div.removeChild(del)
div.removeChild(br)
}
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<body>
<input type="text" value="0" id="idtpiao" ><br />
<form action="?a=1" method="post" >
<input type="button" value="添加" onClick="add()" />
<input type="submit" value="提交" />
<br />
<div id="add">
<input type="text" name="tpiao[]" value="1" id="neir1">
<input type="button" value="删除" id="del1" onClick="del(1)">
<br id="br1" />
<input type="text" name="tpiao[]" value="2" id="neir2">
<input type="button" value="删除" id="del2" onClick="del(2)">
<br id="br2" />
</div>
</form>
</body>
</html>
<?php
$a = isset($_GET['a']) ? $_GET['a'] : "";
if($a == "1"){
print_r($_POST['tpiao']);
}
?>