Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 通过ajax加载包含html内容的JS文件_Jquery_Ajax - Fatal编程技术网

Jquery 通过ajax加载包含html内容的JS文件

Jquery 通过ajax加载包含html内容的JS文件,jquery,ajax,Jquery,Ajax,通过ajax将包含HTML内容的JS文件加载到div,如果我多次加载它,test.JS文件中的JS函数会像加载内容一样运行多次 使用PHP创建HTML内容: $html = ' <script src="js/test.js" charset="utf-8"></script> <div id="form-container" class="p-3"> <label>Teszt</label>

通过ajax将包含HTML内容的JS文件加载到div,如果我多次加载它,test.JS文件中的JS函数会像加载内容一样运行多次

使用PHP创建HTML内容:

$html = '
    <script src="js/test.js" charset="utf-8"></script>
    <div id="form-container" class="p-3">
       <label>Teszt</label>

        <select class="form-control" id="type" name="type">
            <option value="t1">type 1</option>
            <option value="t2">type 2</option>
        </select>

    </div>
';

我尝试使用PHP DateTime添加一个.js文件版本,但它不起作用。

您可以执行以下操作:


找到了解决方案,它使用多个ajax加载多次委托事件处理程序

为了避免这种情况,需要在再次委托事件处理程序之前删除它

$(document).off("change", '#type');

$(document).on("change", '#type', function (event) {
   alert("test");
});

js/test.js文件中有什么?或者“test.js文件看起来怎么样?”@MKougiouris它里面有事件处理程序,我更新了这个问题。
$(document).off("change", '#type');

$(document).on("change", '#type', function (event) {
   alert("test");
});