Javascript事件处理程序在django inline formset中不能正常工作
javascript event handler not correctly work in django inline formset
我设计了如下的内嵌格式集:
class EventForm(ModelForm):
class Meta:
model = Event
exclude = ['created']
class GalleryForm(ModelForm):
class Meta:
model= Gallery
exclude = ['pub_date']
GalleryFormSet = inlineformset_factory(Event, Gallery, extra=0, min_num=1, fields=('title','image' ))
当使用'extra = some number'并正确保存图库表单数据时,它可以正常工作。但是当我使用javascript处理程序在主事件表单下面添加额外的画廊表单时,它创建了两个画廊表单而不是1个,并且数据和照片没有从新添加的表单中保存。
这是我的事件处理程序:
<script type="text/html" id="gallery-template">
<div id="gallery-__prefix__">
{{ formset.empty_form }}
</div>
</script>
<script>
$(function() {
$('.add-photo').click(function(ev){
ev.preventDefault();
var count = parseInt($('#id_gallery_set-TOTAL_FORMS').attr('value'), 10);
var tmplMarkup = $('#gallery-template').html();
var compiledTmpl = tmplMarkup.replace(/__prefix__/g, count)
console.log(compiledTmpl);
$('div.gallery').append(compiledTmpl);
$('#id_gallery_set-TOTAL_FORMS').attr('value', count);
});
});
</script>
My formset template:
<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
{{ formset.management_form }}
{% csrf_token %}
<legend>Events</legend>
<div class="author">
{{ form.as_p}}
</div>
<legend>
<div class="pull-right"><a href="#" class="btn btn-inverse add-photo"><i class="icon-plus icon-white"></i> Add Photo</a></div>
Gallery
</legend>
<div class="gallery form-inline">
{% for form in formset %}
{{ form.as_p }}
{% endfor %}
</div>
<div class="form-group">
<div class="col-sm-offset-6 col-sm-6">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
给你的信息,当我编辑这个格式,然后数据和图像保存正确。
谁能指出我的事件处理程序的错误?
我认为脚本有错误,$('#id_gallery_set-TOTAL_FORMS').attr('value', count);
行应该是:
$('#id_gallery_set-TOTAL_FORMS').attr('value', count + 1);
下面是解决这个问题的可行脚本和模板:
$(function() {
$('.add-photo').click(function(ev){
ev.preventDefault();
var count = parseInt($('#id_gallery_set-TOTAL_FORMS').attr('value'), 10);
var tmplMarkup = $('#gallery-template').html();
var compiledTmpl = tmplMarkup.replace(/__prefix__/g, count)
console.log(compiledTmpl);
$('div.gallery').append(compiledTmpl);
$('#id_gallery_set-TOTAL_FORMS').attr('value', count + 1);
});
});
模板:
<form action="." method="post" enctype="multipart/form-data">
{{ formset.management_form }}
{% csrf_token %}
<legend>Event</legend>
<div class="event">
{{ form}}
</div>
<legend>
<div class="pull-right"><a href="#" class="btn btn-inverse add-photo"><i class="icon-plus icon-white"></i> Add Photo</a></div>
Photo Gallery
</legend>
<div class="gallery form-inline">
{% for form in formset %}
{{ form.as_p }}
{% endfor %}
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
希望对解决相关问题有帮助
相关文章:
- Javascript:selenium Web驱动程序isDisplayed()不工作
- jQuery UI自动完成突然停止工作
- AngularJS UI路由器不能像ng路由器那样工作
- HTML5音频加载和播放获胜'我不能在iPad上工作
- JavaScript打印功能使日历停止工作
- Javascript.getHours()工作不正常
- 为什么这在IE中的工作方式与在Firefox中不同
- 视频HTML没有'无法在Internet Explorer 11上工作
- 扩展移相器按钮类不工作
- Firebase迁移-简单的Firebase.set没有'不再工作了——旧的还是新的
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- 正在尝试使用if和else添加类,但无法正常工作
- Jquery FadeIn FadeOut 只工作一次
- Foreach无法在Typescript中工作
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- JS可以在Chrome中工作,但不能在Firefox中工作
- ajaxToolkit PopupControlExtender不工作.过时的
- HTML标记在脚本标记中工作
- javascript扫雷器floodfill算法不能正常工作