简体   繁体   English

AJAX使用GET方法加载php文件并在加载时但是没有出现一些jquery插件

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

Hallo I beginner learning AJAX and JQuery, Maybe this is a stupid question, but please don't judge me. 你好我开始学习AJAX和JQuery,也许这是一个愚蠢的问题,但请不要评判我。 I create the live showing data, the data is loaded without refresh the page, so I use the AJAX, I load the function onload in body tag. 我创建实时显示数据,加载数据而不刷新页面,所以我使用AJAX,我在body标签中加载函数onload。

I use image picker, it's look good when not using AJAX GET, 我使用图像选择器,不使用AJAX GET时效果很好,

此搜索

and separated my file index.html is the main file, and I also add getdata.php the file show. 并分隔我的文件index.html是主文件,我还添加了getdata.php文件show。

on index.html 在index.html上

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

on script.js 在script.js上

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

on getdata.php 在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>

but the result become like this 但结果变成了这样

图像2

It's like the image-picker not loaded 这就像没有加载的图像选择器

I'm sorry my english is bad. 对不起我的英语不好。

you need to reinitialize the plugin after ajax requests. 你需要在ajax请求后重新初始化插件。

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

remember that, most jquery plugins doesn't apply its effects on dynamically generated html. 请记住,大多数jquery插件不会对动态生成的html应用其效果。

Your script should be the following: 您的脚本应该是以下内容:

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