如何 运行 从另一个页面加载的模式中的脚本?

How to run a script inside a modal that is loaded from another page?

我正在从另一个页面加载我的模式:

("#EditModalBodyDiv").load(url, function () {
                        $("#EditModal").modal("show");
});

我想 运行 模式中的以下脚本,我将在 select 框中设置文本,该文本是来自服务器的变量,但因为模式有尚未加载,变量 (@ViewBag.defactionPlatformName) 未设置:

<script>
    $("#aEditPlatformName option:selected").text(@ViewBag.defactionPlatformName);
</script>

首先,您必须用任意 html 元素替换脚本以发出它,让我们在加载时将其称为 receivedscript,使用:

$("#EditModal").replace(/(<\/?)script([^>]*>)/img,'receivedscript style="display:none!important;"');

加载模式后,您必须执行发出的脚本:

$("#EditModal").find('receivedscript:not([src])').each(function(){
   try {
      eval(this.innerHTML);
   } catch (e) {}
});