简体   繁体   中英

jQuery not replacing some text

I've been working on a project that involves ajax; it's a planner for school assignments. When a button is pressed, it's supposed to change the text inside of 31 <textarea> 's (and one <span> ) based on data it gets from the server. The thing is, textareas that have been changed after the last time the window was refreshed don't change. I've looked over the JSON sent between the server and the webpage and vice-versa, and concluded that the bug is in the success function of the ajax call. Here's the code:

  success: function(data) {                
    $("span#date").text(data['date']);
    $("#assignments").find("textarea").each(function() {
      $(this).text("");
      $(this).html(data[$(this).attr("id")]);
    });
    console.log(data); // I was using this to see if the data received from the server was correct
  }

Thanks very much in advance for any help.

you should use .val() for textarea as it's basically an input.

You can't really have html elements inside it.

Try $(textarea).val() instead of .html() . I noticed that html only works the first time the textarea is rendered.

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