Since it took me a couple of days in order to find a good way to achieve what I wanted, I thought I'd post this Q&A to save others some precious time and unhealthy frustration :3. I simplified the code as much as I could (like removing form action, etc.).
Basically, what I wanted to do is make this :
<form>
<p>
<input type="button" value="Add phone number">
</p>
</form>
become this (by clicking on the button) :
<form>
<div>
<p>
Phone number : <input type="text" name="phone_number1">
<input type="button" id="remove_phone_number1" value="Remove">
</p>
</div>
<p>
<input type="button" value="Add phone number">
</p>
</form>
and, if I click one more time, it would become this :
<form>
<div>
<p>
Phone number : <input type="text" name="phone_number1">
<input type="button" id="remove_phone_number1" value="Remove">
</p>
</div>
<div>
<p>
Phone number : <input type="text" name="phone_number2">
<input type="button" id="remove_phone_number2" value="Remove">
</p>
</div>
<p>
<input type="button" value="Add phone number">
</p>
</form>
(all that with a working Remove button, of course)
I thought doing such a thing was pretty straightforward and easy, but I had a really hard time finding a solution.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…