简体   繁体   中英

Jquery Variable Not Passing

I'm having an issue where I cannot seem to pass the variable "notes" into the placeholder of a form. More Info Below Code:

   request.done(function(data) {

                   window.notes = data;
                   //document.write(notes);

                });
                request.fail(function() {
                   // document.write("fail");
                                     });

                $(document).ready(function(){
                    $('form').find("textarea").each(function(ev)
                    {
                        if(!$(this).val()) {


                           // document.write(notes); Says undefined?
                            $(this).attr("placeholder",window.notes);
                        }
                    });
                });

The placeholder code works...if I use

  $(this).attr("placeholder","test");

It works without problem as it should, but it will not if I try to use the var notes.

The setting of var notes works as well. If I uncomment the document.write("notes") I get the value I expect.

I have tried:

I originally started with Var notes = data; but moved to window.notes based on a suggestion by another question to make it global. Did not work. I also tried "forcing" the variable to be global by setting outside the function scope empty, but that didn't work either.. Is there something I'm missing?

Thanks!

Sorry I don't have much credit to comment . I just wanted to say that the document.ready function works on load at the starting of the page load while request.done or request.fail works after it so initially on load the window.notes is undefined. And when you use the code document.write(notes) it gets defined and hence no error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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