我需要解决这个问题:
我有一个 django 站点,它在食谱列表中显示所有食谱
这些配方中的每一个都是一个链接,单击该链接时应将该配方的字段加载到 popupdiv 中
到目前为止,我已经设置了弹出 div - 现在我只需要弄清楚如何访问该按钮的配方字段。
基本上我想加载的内容是 recipe.name recipe.author recipe.steps 等,但我不知道该怎么做。
现在我也有一个表单正在使用 ajax 加载到 div 中,所以我不想干扰 ajax 调用(我也会发布此代码)。对这样的问题也使用 ajax 是否有意义?
这是我的按钮点击功能
$(document).ready(function(){
$(".button").click(function(){
var content = $("#popupContact").load($('#recipe'));
});
});
这是我页面的模板:
{% block content %}
{% autopaginate recipe_list 6 %}
<div id="recipe_cont">
{% for recipe in recipe_list %}
<div class="recipe">
<div class="button">//here is the button that is clicked to load recipe
<a href="{% url cookbook.views.userrecipe recipe.id %}" style="display: none;"></a>
<img src="{{ STATIC_URL }}chicknbraw.jpg" alt="" height="70" width="70" style="display:inline;" />
<h4>{{ recipe.name }}</h4>
</div>
<h5>{{ recipe.author }}</h5>
<h5>Prep Time: {{ recipe.prep_time }} minutes</h5>
<h6><a href="/addrecipe/{{ recipe.id }}">Add Recipe</a>
<a href="/removerecipe/{{ recipe.id }}">Remove Recipe</a></h6>
</div>
{% endfor %}
</div>
<div id="popupContact" class="popup">//load recipe information into this div
<a id="popupContactClose" style="cursor:pointer;float:right;">x</a>
<p id="contactArea">
<h1 style="text-align:center">Create New Recipe</h1>
{% include 'cookbook/create_form.html' %} //this is for the form that is loaded. Is there a way to include a different template? Maybe have multiple contactAreas and hide/show different ones depending on what is being loaded?
</p>
</div>
<div id="backgroundPopup">
</div>
<div id="col2-footer">
{% paginate %}
<p id="recipe_order_text"> order by: <a href="/userbook/ordered/name">abc</a>|<a href="/userbook/ordered/date">date</a>
</div>
{% endblock %}
这是表单ajax调用:
$(document).ready(function(){
function hijack() {
var form = $('form#createrecipeform');
form.each (function(){
this.reset();
});
form.submit(function(e) {
e.preventDefault();
console.log('ajax form submission function called successfully.');
//form = $(this);
console.log(form)
var serialized_form = form.serialize();
$.ajax({ type: "POST",
url: $(this).attr('action'),
data: serialized_form,
success: (function(data) {
console.log('ajax success function called successfully.');
data = $.parseJSON(data);
if (data.success) {
console.log('success');
} else {
console.log('failure');
var newForm = data.form;
form.replaceWith(newForm);
hijack();
}
})
});
return false;
});
};
hijack();
});
非常感谢您提供的任何解决方案
凯蒂