gpt4 book ai didi

bootstrap-modal - Bootstrap 模态表单在提交错误时消失

转载 作者:行者123 更新时间:2023-12-01 14:17:40 24 4
gpt4 key购买 nike

我刚刚将表单转换为 Bootstrap 模态,它工作正常,除非提交时出现错误,模态会消失。单击模态触发按钮会将模态与用户数据和错误消息一起备份。

这是我的代码:

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" 
aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<form method='POST' action=''>
{% csrf_token %}
{{ form.as_p }}
<input id='submit-button' type="submit" value="Submit">
</form>
</div> <!-- modal-body -->
</div>
</div>
</div>

<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Apply now</button>

我正在使用 Django,但我认为这与它没有任何关系,因为在我将表单包装在模式中之前一切正常。这是我的 view.py:

def homepage(request):
form = MyForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
instance.user = request.user
instance.save()
return HttpResponseRedirect(reverse('success'))
context = {
"form": form,
}
return render(request, 'home.html', context)

谁能告诉我我做错了什么?

最佳答案

提交后没有modal的信息,和第一次打开页面时一样隐藏。如果出现错误,您可以添加一些条件来打开模态。

我不了解 Django,但我的想法是向模型添加一些参数:

if (bindingResult.hasErrors()) {
model.addAttribute("formErrors", true);
}

然后检查(我在这个例子中使用 Thymeleaf):

<th:block th:if="${formErrors}">
<script>$('#myModal').modal('show');</script>
</th:block>

关于bootstrap-modal - Bootstrap 模态表单在提交错误时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38874671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com