javascript - How can I refresh a div with a button click? - Stack Overflow

I have this ajax call.It populates my page when opened.How can I reload it again for a new query by c

I have this ajax call. It populates my page when opened. How can I reload it again for a new query by clicking a button?

I tried to figure it out myself, but I can't.

Script:

$(document).ready(function() {
    $("#loading").show();
    $.getJSON("listapps.php",function(data) {
        $.each(data, function(i,data) {
            data.title = data.title.replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, '-').replace(/-+/g,'-');
            if (data.title =='' | data.title =='-') {
                data.title = 'App';
            }
            var div_data = "<div ><a href='"+data.title+"-"+data.appID+"'><img src='"+data.icon+"'></img></a></div>";
            $(div_data).appendTo("#iconsfree");
        });
        $("#loading").hide();
    });
    return false;
});

HTML:

<div id='loading'>Loading...</div>
<div id='iconsfree'></div>

I have this ajax call. It populates my page when opened. How can I reload it again for a new query by clicking a button?

I tried to figure it out myself, but I can't.

Script:

$(document).ready(function() {
    $("#loading").show();
    $.getJSON("listapps.php",function(data) {
        $.each(data, function(i,data) {
            data.title = data.title.replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, '-').replace(/-+/g,'-');
            if (data.title =='' | data.title =='-') {
                data.title = 'App';
            }
            var div_data = "<div ><a href='"+data.title+"-"+data.appID+"'><img src='"+data.icon+"'></img></a></div>";
            $(div_data).appendTo("#iconsfree");
        });
        $("#loading").hide();
    });
    return false;
});

HTML:

<div id='loading'>Loading...</div>
<div id='iconsfree'></div>
Share Improve this question edited Feb 4, 2015 at 18:12 Artur Filipiak 9,1574 gold badges32 silver badges56 bronze badges asked Feb 4, 2015 at 17:44 HunnenkoenigHunnenkoenig 1991 gold badge3 silver badges14 bronze badges 1
  • Does this answer your question? Onclick reload the div only – Jack Commented Jul 8, 2020 at 11:36
Add a ment  | 

2 Answers 2

Reset to default 1

Make a method, call it when page is opened and on button click.

Script:

$(document).ready(function() { 

    function refresh(){
        $("#loading").show();
        $.getJSON("listapps.php",function(data) {
            var div_data = '';
            $.each(data, function(i,data) {
                data.title = data.title.replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, '-').replace(/-+/g,'-');
                if (data.title =='' | data.title =='-') {
                    data.title = 'App';
                }
                div_data += "<div ><a href='"+data.title+"-"+data.appID+"'><img src='"+data.icon+"'></img></a></div>";
            });
            $("#iconsfree").html(div_data);
            $("#loading").hide();
        });
    }

    // call the method on button click:
    $(document).on('click', '#button', function(e){
        e.preventDefault();
        refresh();
    });

    // call the method when page is opened:
    refresh();

});

Attach it to the click() event of some element.

Your HTML:

<a id="button" href="#">Reload</a>

Your JavaScript:

$(document).ready( function(){
    $("#button").click( function(){
        $("#loading").show();
        $.getJSON("listapps.php",function(data) {
             $.each(data, function(i,data) {
                 data.title = data.title.replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, '-').replace(/-+/g,'-');
                  if (data.title =='' | data.title =='-') {
                      data.title = 'App';
                  }

                  var div_data =
                    "<div ><a href='"+data.title+"-"+data.appID+"'><img src='"+data.icon+"'></img></a></div>";
                   $(div_data).appendTo("#iconsfree");
              });
              $("#loading").hide();
        });
        return false;
    });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744798813a4594366.html

相关推荐

  • javascript - How can I refresh a div with a button click? - Stack Overflow

    I have this ajax call.It populates my page when opened.How can I reload it again for a new query by c

    2天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信