簡體   English   中英

AJAX使用GET方法加載php文件並在加載時但是沒有出現一些jquery插件

[英]AJAX load php file using GET method and when loaded but some jquery plugin is not appear

你好我開始學習AJAX和JQuery,也許這是一個愚蠢的問題,但請不要評判我。 我創建實時顯示數據,加載數據而不刷新頁面,所以我使用AJAX,我在body標簽中加載函數onload。

我使用圖像選擇器,不使用AJAX GET時效果很好,

此搜索

並分隔我的文件index.html是主文件,我還添加了getdata.php文件show。

在index.html上

<div class="col-xs-8 picker" id="viewdata">
     // content getdata.php here                
</div>

在script.js上

function viewdata(){
   $.ajax({
   type: "GET",
   url: "php/getdata.php",
   dataType: "html"
  }).done(function( data ) {
  $('#viewdata').html(data);
  });
}

在getdata.php上

<select class="image-picker">
        <?php
            include "config.php";
            $res = $conn->query("select * from images");
            while ($row = $res->fetch_assoc()) {
            ?>
        <option data-img-src="images/<?php echo $row['file_name'];?>" value="<?php echo $row['file_name'];?>"> <?php echo $row['file_name'];?> </option>
        <?php } ?>
    </select>

但結果變成了這樣

圖像2

這就像沒有加載的圖像選擇器

對不起我的英語不好。

你需要在ajax請求后重新初始化插件。

$.ajax({
    type: "GET",
    url: "php/getdata.php",
    dataType: "html"
}).done(function( data ) {
    $('#viewdata').html(data);
    $("select").imagepicker(); //reinitialize here
});

請記住,大多數jquery插件不會對動態生成的html應用其效果。

您的腳本應該是以下內容:

function viewdata(){
    $.ajax({
        type: "GET",
        url: "php/getdata.php",
        dataType: "html",
        success: function(data){
            $('#viewdata').html(data);
            $('select').imagepicker(); // to reinitialize the plugin.
        }
        error: function(data){
            // error handling
        }
    }).done(function(data){
        // something to do after, even if it throws an error.
    });
}

暫無
暫無

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

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