how to know when the DOM is ready again after adding a node from ajax

前端 未结 2 1648

I receive a JSON-File via ajax and added it to my DOM (see: how to read information of an ajax-dialogbox)

Now i wanted to get access to this DOM-node, but the only way i

2条回答
  •  遇见更好的自我
    2021-01-24 06:22

    I am pretty sure it's because of the asynchronous ajax call. You are trying to access a variable which has not yet been set.

    If you put an alert before the actual access, it takes some time to click the ok button, so the call is being completed. When the code goes to the next line it works as expected because the value is set.

    You shuld set your variable / do something with it in your coallback function. Since you didn't post any of your actual code, I'll improvise:

    var yourVar;
    $.get("someurl", params, function (data) {
      yourVar = data; // Let's say that you set it right here
      alert(yourVar); // will work perfectly
    });
    alert(yourVar); // Possibly won't work because it will most likely be called before the get is completed
    

    EDIT: I finished writing this answer before you posted your code. It appears this is the case but I'll look into it more deeply to confirm.

    success: function(response) {           
        response = response.replace(/.*?;{/, "{");                          //Entfernt for(;;;)
        jsonFile = JSON.parse(response);                                    //Parsed den Text in ein Json file                   
        $('#globalContainer').append(jsonFile.payload.body.__html);         //Fügt das Dialogfenster ganz unten an die Seite hinzu
        // Here it should work
        alert("Test Combo" + combobox_by_name(value.ID_of_name));
    },
    

    You can also look into getJSON method since it's a shorthand I think you'll find useful in your case. It returns actual JSON data so you don't need to do any black-magic parsing.

提交回复
热议问题