[英]javascript on the main page is not working in the dynamically loaded content
我通过ajax get加载内容,进入div,我有3页:
索引通过ajax javascript.js加载的动态内容
现在,当我通过以下方式将数据加载到索引中时:
$.ajax({
url: target,
type: 'GET',
}).done(function(data)
{
$(".modal-content").html($(data).find('.inner_modal'));
$(".modal-header").prepend('<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>');
});
});
即使我在主索引中包含了javascript文件,JavaScript也无法在其上运行。
我已经尝试过像这样绑定:
$(function()
{
$('.inner_modal').on("submit", "#account-login-form", function()
{
$('.ajloading').show();
$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method'),
dataType: 'json',
data: $(this).serialize(),
success: function(data)
{
if(!data.success)
{
$('#error_message').html(data.error + alert_close).show();
}else{
<?php if(Session::get('refer') == true && Session::get('refer') != 'user/logout'): ?>
window.location.href = '<?php echo Config::get('URL'); ?><?php echo System::escape(Session::get('refer')); ?>';
<?php else: ?>
window.location.href = '<?php echo Config::get('URL'); ?>dashboard';
<?php endif; ?>
}
$('.ajloading').hide();
}, error: function(data)
{
$('.ajloading').hide();
}
});
return false;
});
});
我已经这样尝试过了:
$(document).on("submit", "#account-login-form", function()
我也试过了
$(function(){});
现在,当我在动态加载的内容中解开javscript文件时,它可以工作,但是我不能对每个文件都这样做,而且它会使脚本would肿,因此如何解决此问题,以便动态内容可以与我的主要js文件一起使用。 ...这是我的做法:
<body>
<div class="modal-content">
<!-- dynamic content loaded !-->
</div>
</body>
<script>
$.ajax({
url: target,
type: 'GET',
}).done(function(data)
{
$(".modal-content").html($(data).find('.inner_modal'));
$(".modal-header").prepend('<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>');
});
});
</script>
<script src="myjs.js"></script>
</html>
我认为您的问题是“提交”不是一个冒泡的事件。 通过ajax加载表单后,应立即为表单分配一个点击处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.