以php格式删除特定添加的字段

时间:2014-02-08 10:03:59

标签: php forms

我有一个可以创建字段的表单,但是如何从我创建的表单中删除添加的字段?甚至删除特定字段?对不起,我对此有点新鲜。 如果可能的话,有没有办法只删除我添加的字段的一行,旁边有一个十字按钮或什么?

表格

 <script type="text/javascript">
function addTextArea(){
var div = document.getElementById('name');
div.innerHTML += "<input type='text' name='name[]' value='' />";
div.innerHTML += "\n<br />";
var div = document.getElementById('quantity');
div.innerHTML += "<input type='text' name='quantity[]' value ='' />";
div.innerHTML += "\n<br />";
var div = document.getElementById('amount');
div.innerHTML += "<input type='text' name='amount[]'  value =''/>";
div.innerHTML += "\n<br />";
}

</script>


</head>

<body>

<form method="POST" action="confirm_invoice.php" >
<?php
echo "<table border='2'>\n"; 
echo "<tr>\n"; 
echo "<th>Description</th>\n"; 
echo "<th>Quantity</th>\n"; 
echo "<th>Amount($)</th>\n";
 echo "</tr>"; 

 echo "<tr>";
 echo "<td>"?><input type='text' size="50" name='name[]' value='Examination and Consultation' readonly/><?php "</td>";
 echo "<td>"?><input type='text' size="50" name='quantity[]' value='' /><?php "</td>";
 echo "<td>"?><input type='text' size="50" name='amount[]' value='' /><?php "</td>";
 echo "</tr>";

echo "<tr>"; 
 echo "<td>"?><div id="name"></div> <?php "</td>"; 
 echo "<td>"?><div id="quantity"></div> <?php "</td>"; 
 echo "<td>"?><div id="amount"></div> <?php "</td>"; 
 echo "</tr>";
 ?>
 <br />
<input type="button" value="Add Description" onClick="addTextArea();"> &nbsp;
<input type="submit" name="submit" value="submit">
</form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

你可以尝试这个,工作,需要微调一下。

<script type="text/javascript">

var count = 0;

function addTextArea(){
count= count+1;
var div = document.getElementById('name');
div.innerHTML += "<div> <input type='text' name='name[]' value='' "+"id=name"+count+"> </div>";
//div.innerHTML += "\n<br />";
var div = document.getElementById('quantity');
div.innerHTML += "<div><input type='text' name='quantity[]' value ='' "+"id=quantity"+count+"></div>";
//div.innerHTML += "\n<br />";
var div = document.getElementById('amount');
div.innerHTML += "<div><input type='text' name='amount[]'  value ='' "+"id=amount"+count+"></div>";
//div.innerHTML += "\n<br />";
}

function removeTextArea(){
document.getElementById("name"+count).remove();

document.getElementById("quantity"+count).remove();

document.getElementById("amount"+count).remove();

count = count-1;
}

</script>


</head>

<body>

<form method="POST" action="confirm_invoice.php" >
<?php
echo "<table border='2'>\n"; 
echo "<tr>\n"; 
echo "<th>Description</th>\n"; 
echo "<th>Quantity</th>\n"; 
echo "<th>Amount($)</th>\n";
echo "</tr>"; 

echo "<tr>";
echo "<td>"?><input type='text' size="50" name='name[]' value='Examination and Consultation' readonly/><?php "</td>";
echo "<td>"?><input type='text' size="50" name='quantity[]' value='' /><?php "</td>";
echo "<td>"?><input type='text' size="50" name='amount[]' value='' /><?php "</td>";
echo "</tr>";

echo "<tr>"; 
echo "<td>"?><div id="name"></div> <?php "</td>"; 
echo "<td>"?><div id="quantity"></div> <?php "</td>"; 
echo "<td>"?><div id="amount"></div> <?php "</td>"; 
echo "</tr>";
?>
<br />
<input type="button" value="Add Description" onClick="addTextArea();"> &nbsp;
<input type="submit" name="submit" value="submit">

<input type="button" value="Remove Description" onClick="removeTextArea();"> &nbsp;
<input type="submit" name="submit" value="submit">

</form>

</body>
</html>