i wonder best way create dynamic form django.
let's there training form can embrace workouts. number of workouts unspecified (1+). how create such form button adding new workout it?
edit:
my backend written in django/python. far, models looks below:
class workoutform(forms.form): name = forms.charfield(max_length = 45) description = forms.charfield(widget = forms.textarea) class trainingform(forms.form): name = forms.charfield(max_length = 45) all_workouts = workout.objects.all() description = forms.charfield(widget = forms.textarea) selected_workouts = forms.multiplechoicefield(required=false, widget=forms.selectmultiple, choices=[(o.id, o.name) o in all_workouts])
but want change dynamic form new workout field added after clicking add button.
does you?
$(function () { $("#add").click(function (e) { e.preventdefault(); // make sure nothing happens. $(".master") .clone() // clone .removeclass("master") // remove master class .find("input").attr("name", "workout[]").end() // change input name .appendto("form"); // append form. }); });
label {display: block;} .master {display: none;}
<script src="https://code.jquery.com/jquery-2.1.4.js"></script> <form action=""> <label class="master"> <strong></strong> <input type="text" /> </label> <a href="#" id="add">add new</a> <label> <strong></strong> <input type="text" name="workout[]" /> </label> </form>
Comments
Post a Comment