簡體   English   中英

主頁上的javascript在動態加載的內容中不起作用

[英]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">&times;</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">&times;</button>');
        });
    });
</script>
<script src="myjs.js"></script>
</html>

我認為您的問題是“提交”不是一個冒泡的事件。 通過ajax加載表單后,應立即為表單分配一個點擊處理程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM