我想使用Javascript自动将新表单添加到Django表单集中,这样当用户单击“添加”按钮时,它会运行JavaScript,向页面添加新表单。
答案 0 :(得分:1)
查看我使用的代码,
<script type="text/javascript">
$(document).ready(function(e){
$(".add-more a").click(function(e){
e.preventDefault();
var id_groupmembers_set = $('#id_groupmembers_set-TOTAL_FORMS').val();
$(".personal-info-grp ul.user-name").append($('#empty_form').html().replace(/__prefix__/g, id_groupmembers_set));
$('#id_groupmembers_set-TOTAL_FORMS').val(parseInt(id_groupmembers_set) + 1);
});
$(".signin-form a.cross-btn").live("click", function(e){
e.preventDefault();
$(this).parent().parent().remove();
});
});
</script>
您可以在脚本中使用您的表单ID和所有内容。
示例表单就像这样
<div id="empty_form" style="display:none">
<li>
<div class="signin-form clearfix">
<div class="clearfix">
<label><span>{% trans 'Name' %}<b>*</b></span>:</label>
{{ members_form.empty_form.first_name }}{{ members_form.empty_form.name.errors.as_text }}
</div>
<div class="clearfix">
<label><span>{% trans 'Age' %}<b>*</b></span>:</label>
{{members_form.empty_form.age}}{{members_form.empty_form.age.errors.as_text}}
</div>
<div class="clearfix">
<label><span>{% trans 'Gender' %}<b>*</b></span>:</label>
{{members_form.empty_form.gender}}{{members_form.empty_form.gender.errors.as_text}}
</div>
<a href="#" class="cross-btn"><img src="/static/assets/images/cross-btn.png" alt="" /> </a>
</div>
</li>
</div>
利用它;)