结合Flask/Jinja的jQuery/Ajax插入DOM元素置灰不渲染问题咨询
关于jQuery/Ajax插入模态框元素无法正常渲染的问题梳理
核心问题与诉求
- 此前发布的同主题StackOverflow提问因缺少最小可复现示例,被建议补充相关内容。核心开发需求为:将jQuery/Ajax请求返回的HTML结果中的新增节点渲染到当前模态框,由于页面HTML是通过Flask/Jinja动态生成的,无法为每个插入的节点单独编写定位逻辑。
- 当前开发异常:jQuery/Ajax流程执行无报错,目标元素确实已被插入DOM,但在浏览器开发者工具中检查时,这些插入的元素呈灰色状态,未被浏览器正常渲染。需要在不引入额外库的前提下优化实现逻辑,让插入的节点能被浏览器识别并正常渲染。
最小复现示例搭建的疑问
- 作为编程新手,尝试使用CodePen这类在线工具整理代码,方便在提问时附上演示链接,但不清楚如何在这类工具中放置多份HTML代码,也无法模拟完整的Flask应用流程。
- 之前上传的CodePen项目存在结构冲突:缺少Flask后端文件,且CodePen要求index.html存放在根目录,和本地Flask项目要求模板文件存放在templates文件夹的规则不一致,希望能得到更适合在StackOverflow提问时复现场景的工具推荐。
现有实现代码
modals.js
$(document).ready(function () { $('#frmAddProduct').on('submit', function aj(event) { event.preventDefault(); $.ajax({ cache: false, type: 'POST', url: '/products/new', data: $('#frmAddProduct').serialize(), datatype: 'html' }) .done(function process(data) { /* The modal has to be shown unless the user clicks on 'close'*/ console.log(data); $('#modalAdd').modal('show'); let err = $(data).find(".invalid-feedback").html(); if (err) { /* replace the contents of the modal with section from ajax result to show validation messages*/ let cont = $(data).find("#targetModalContent").html(); // let cont = $(data).find("#targetModalContent").parent().html(); // console.log(cont); $('#targetModalContent').html(cont); // $('#targetModalContent').replaceWith(cont); } else { /* Show success flash message on the modal instead of the original below nav location --to do*/ console.log(data); } }); }); });
products.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Bootstrap CSS Offline --> <link href="static/css/bootstrap.css" rel="stylesheet"> <!-- Bootstrap JS Offline--> <script src="static/js/jquery-3.5.1.js"></script> <script src="static/js/popper.min.js"></script> <script src="static/js/bootstrap.js"></script> <title>Products</title> </head> <body> <h2> <button type="button" class="btn btn-success" data-toggle="modal"data-target="#modalAdd"> Add New Product </button> </h2> <!-- ModalAdd --> <div class="modal fade" id="modalAdd" tabindex="-1" role="dialog" aria-labelledby="modalAddTitle" aria-hidden="true"> <div class="modal-dialog modal-dialog-centered modal-lg" role="document"> <div class="modal-content" id="targetModalContent"> <div class="modal-header"> <h5 class="modal-title" id="modalAddLongTitle">Add New Item</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <form method="POST" action="" id="frmAddProduct"> <div class="modal-body"> <div class="container-fluid"> <fieldset class="form-group"> <div class="form-group row"> <label class="col-form-label col-sm-5" for="description">Product Description</label> <input class="form-control col-sm-7" id="description" name="description" type="text" value=""> </div> </fieldset> </div> </div> <div class="modal-footer"> <div> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> </div> <div> <input class="btn btn-primary" id="submit" name="submit" type="submit" value="Add Product"> </div> </div> </form> </div> </div> </div> <script src="static/js/modals.js"></script> </body> </html>
results.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Bootstrap CSS Offline --> <link href="static/css/bootstrap.css" rel="stylesheet"> <!-- Bootstrap JS Offline--> <script src="static/js/jquery-3.5.1.js"></script> <script src="static/js/popper.min.js"></script> <script src="static/js/bootstrap.js"></script> <title>Products</title> </head> <body> <h2> <button type="button" class="btn btn-success" data-toggle="modal" data-target="#modalAdd"> Add New Product </button> </h2> <!-- ModalAdd --> <div class="modal fade" id="modalAdd" tabindex="-1" role="dialog" aria-labelledby="modalAddTitle" aria-hidden="true"> <div class="modal-dialog modal-dialog-centered modal-lg" role="document"> <div class="modal-content" id="targetModalContent"> <div class="modal-header"> <h5 class="modal-title" id="modalAddLongTitle">Add New Item</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <form method="POST" action="" id="frmAddProduct"> <div class="modal-body"> <div class="container-fluid"> <fieldset class="form-group"> <div class="form-group row"> <label class="col-form-label col-sm-5" for="description">Product Description</label> <input class="form-control col-sm-7" id="description" name="description" type="text" value=""> <div class="invalid-feedback"> <span class="col-sm-7 offset-5">Product combination is already enrolled.</span> </div> </div> </fieldset> </div> </div> <div class="modal-footer"> <div> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> </div> <div> <input class="btn btn-primary" id="submit" name="submit" type="submit" value="Add Product"> </div> </div> </form> </div> </div> </div> <script src="static/js/modals.js"></script> </body> </html>
app.py
from flask import Flask, render_template, redirect app = Flask(__name__) @app.route('/') @app.route('/products') def index(): return render_template("products.html") @app.route('/products/new', methods=['GET', 'POST']) def add(): return render_template("results.html") if __name__ == '__main__': app.run()
补充说明
- 浏览器页面截图可直观观察到插入元素呈灰色未渲染的状态。
- 此前检索到的StackOverflow相似问题,均无法匹配当前遇到的场景。
- 按照平台建议整理了HTML与JS的内置代码片段,但目前无法在片段编辑器中正常显示模态框,也无法模拟AJAX请求流程。
内容的提问来源于stack exchange,提问作者aik3e
相关产品推荐
相关产品推荐

