[英]AJAX response not running function placed in external JS file (running only when placed directly in AJAX response)
I have a problem with AJAX response not running a simple Jquery function, that is placed in external JS file. 我对AJAX响应未运行简单的Jquery函数(放置在外部JS文件中)有问题。 The function runs only when I put it's code directly to AJAX response view code. 仅当我将其代码直接放入AJAX响应视图代码时,该函数才会运行。
The page, which displays link for dynamically loaded AJAX request, has embedded JS file in header: 该页面显示了动态加载的AJAX请求的链接,该页面在标头中嵌入了JS文件:
<script type="text/javascript" src="script.js"></script>
In "script.js" file I have two functions: 1. One for loading AJAX response from "/some_url" in "#some_id" element. 在“ script.js”文件中,我有两个功能:1.一个用于从“ #some_id”元素中的“ / some_url”加载AJAX响应。 2. Second for using in AJAX response - which only alerts "a", when the loaded form is submitted 2.在AJAX响应中使用的第二个-仅在提交加载的表单时警告“ a”
jQuery(function () {
$('.ajax_link').click(function(event) {
$.ajax({
type: 'GET',
url: '/some_url',
evalScripts: true,
cache: false,
dataType: 'html',
success: function(data){
if(parseInt(data)!=0 && data!='') {
$('#some_id').html(data);
}
}
});
});
});
jQuery(function () {
$('.ajax_form').submit(function(event) {
event.preventDefault();
alert('a');
});
});
In the main page (which has "script.js" file embeded in header) I have following code: 在主页中(在标题中嵌入了“ script.js”文件),我有以下代码:
<div><a class="ajax_link">Load AJAX in some_id</a></div>
<div id="some_id"></div>
Clicking on the link successfully loads the AJAX response to "some_id" div. 单击链接成功将AJAX响应加载到“ some_id” div。
Now, in the AJAX response, I have a simple form: 现在,在AJAX响应中,我有一个简单的表格:
<form class="ajax_form" method="post" action="/some_other_url">
<input class="submit" type="submit" value="Send"/>
</form>
Now, if I don't place the piece of script, dedicated to "ajax_form" class, under the form code - it does not work at all. 现在,如果我不将专用于“ ajax_form”类的脚本放在表单代码下,则根本不起作用。 If I place the following code (so basically, I repeat the same function as in "script.js" file) 如果放置以下代码(基本上,我重复与“ script.js”文件中的功能相同的功能)
<script type="text/javascript">
jQuery(function () {
$('.ajax_form').submit(function(event) {
event.preventDefault();
alert('a');
});
});
</script>
in the AJAX response view (for example, under the ) it works. 在AJAX响应视图中(例如在下方),它可以工作。
Why is that? 这是为什么? Isn't AJAX response using the JS files, loaded to the "main" page? AJAX响应不是使用加载到“主”页面上的JS文件吗?
I would be really apriciate for help :) 我真的很乐于助人:)
Guess: 猜测:
What seems to be happening is when your script.js
gets loaded/executed there is no .ajax_form
element present in DOM. 似乎正在发生的事情是,当您的script.js
被加载/执行时,DOM中不存在.ajax_form
元素。 When you place the code below you form
tag it gets executed again and works. 当您将代码放在form
标签下面时,它会再次执行并起作用。
Try updating your script.js
with below code: 尝试使用以下代码更新您的script.js
:
jQuery(function () {
$( document ).on( "submit", ".ajax_form", function( event ) {
event.preventDefault();
alert('a');
});
});
Have a look at event delegation . 看一下事件委托 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.