我有一个带有 2 个变量的 jinja 模板(其中一个在 for 循环中(,我想将它们传递给引导模式以确认在循环中触发的删除。 它的架构是这样的:
<object_outside_loop>
<!-- Loop -->
{% for item_inside_loop in items %}
<button type="button" data-toggle="modal">Delete</button>
{% endfor %}
<!-- Modal -->
<my-modal-here>
<a href="{{ url_for('delete_something', id1=object_outside_loop._id, id2=item_inside_loop._id) }}" >Confirm Delete</a>
</my-modal-here>
我需要将两个变量(id1 和 id2(从模态传递到我的路由:
@app.route("/delete_something/<id1>/<id2>")
def delete_something(id1, id2):
mongo.db.something.update_one(
{"_id": ObjectId(id2)},
{"$pull": {"something": {"_id": ObjectId(id1)}}},
)
flash("Your something has been updated!", "success")
return redirect(url_for("somewhere", id=id2))
如何将这两个变量传递给模态,以及如何在不破坏端点的情况下做到这一点?
请尝试以下操作...
假设:您的模态id
是my-modal
更改Delete
按钮的显示方式:
{% for item_inside_loop in items %}
<button type="button" data-toggle="modal" data-target="#myModal" data-url="{{url_for('delete_something', id1=object_outside_loop._id, id2=item_inside_loop._id)}}">Delete</button>
{% endfor %}
将Delete
按钮更改为:
<a id="confirm-delete">Confirm Delete</a>
现在,单击模态中的Delete
按钮,将其更改为:
$(document).ready(function(){
$("#my-modal").on("show.bs.modal", function(event){
// Get the button that triggered the modal
var button = $(event.relatedTarget);
// Extract value from the custom data-* attribute
var url = button.data("url");
$(this).find('#confirm-delete').attr('href', url)
});
});
我希望以上内容对您有用。