简体   繁体   English

无法更改

[英]can't change text of a

I have a javascript function that creates a lot of panels using an ajax request. 我有一个javascript函数,可以使用ajax请求创建很多面板。 After the request gives me a valid json , I populate all the widgets like this: 在请求给我一个有效的json ,我像这样填充所有小部件:

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: 我还有另一个函数可以更改小部件内某些div对象的.text()

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 . 出于某种原因,在使用ajax填充时无法找到/使​​用id When the widget is not created with ajax (but in static html ) it does work.. 如果未使用ajax创建小部件(但使用静态html创建),则它确实起作用。

EDIT: I made a callback in the function that generates the widgets: 编辑:我在生成小部件的函数中进行了callback

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.. 但这仍然行不通..

ajax is asynchronous, meaning that the done handler will be called after the function returns. ajax是异步的,这意味着完成的处理程序将在函数返回后调用。 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: 更好的方法是使用jqXHR事件-像完成一样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM