Jquery 通过ajax加载包含html内容的JS文件
通过ajax将包含HTML内容的JS文件加载到div,如果我多次加载它,test.JS文件中的JS函数会像加载内容一样运行多次 使用PHP创建HTML内容: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>
$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");
});