weixin_33725270 2017-12-23 22:51 采纳率: 0%
浏览 6

无法更改

I have a javascript function that creates a lot of panels using an ajax request. After the request gives me a valid json, I populate all the widgets like this:

function createAllWidgets() {
    var funcid = 'get_widget_info';
    var jqxhr = $.ajax({
        type: 'GET',
        url: 'functions/call_functions.php',
        data: {
            funcid: funcid
        },
        dataType: 'json'
    }).done(function(myData) {

        if (myData == null) {
            alert('something went worng');
        } else {
          var html = '';
          for (var i=0;i<myData.length;i++) {
                html += '<h3 id="' + myData[i].widget_abb_upper + '-EUR" class="text-primary counter m-t-10">0</h3>'
                     +  '<h3 id="' + myData[i].widget_abb_upper + '-USD" class="text-primary counter m-t-10">0</h3>';
            }
            $('#widgets').html(html);
        }
    })
    .fail(function() {alert('something went worng');}); 

}

I have another function that changes the .text() of some of the div objects within the widget:

function setWidget(priceUSD, priceEUR, widget) {
    $('#' + widget + '-EUR').text(priceEUR);
    $('#' + widget + '-USD').text(priceUSD);
}

For some reason the id can not be found/used when populated with ajax. When the widget is not created with ajax (but in static html) it does work..

EDIT: I made a callback in the function that generates the widgets:

function createAllWidgets(callback) {
    var funcid = 'get_widget_info';
    var jqxhr = $.ajax({
        type: 'GET',
        url: 'functions/call_functions.php',
        data: {
            funcid: funcid
        },
        dataType: 'json'
    }).done(function(myData) {

        if (myData == null) {
            alert('something went worng');
        } else {
          var html = '';
          for (var i=0;i<myData.length;i++) {
                html += '<h3 id="' + myData[i].widget_abb_upper + '-EUR" class="text-primary counter m-t-10">0</h3>'
                     +  '<h3 id="' + myData[i].widget_abb_upper + '-USD" class="text-primary counter m-t-10">0</h3>';
            }
            $('#widgets').html(html);
        }
    })
    .fail(function() {alert('something went worng');}); 

}

Then I made a new function to call the above two functions:

function initStart() {
    createAllWidgets(function() {
        setWidget(1,1,'widget1');
    });
}

But this still doesn't work..

  • 写回答

1条回答 默认 最新

  • weixin_33695082 2017-12-24 11:01
    关注

    ajax is asynchronous, meaning that the done handler will be called after the function returns. Your solution of adding a callback doesn't work because you haven't called it.

    If you still want to use a callback, add this:

    $('#widgets').html(html); // This is yours
    callback();               // <- Add this
    

    A better way, is to use the jqXHR events - like done:

    function initStart() {
        createAllWidgets().done(function() {
            setWidget(1,1,'widget1');
        });
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改