Javascript 将文本字段添加到文本区域
我正在尝试开发应用程序,但无法填写以下文本区域:Javascript 将文本字段添加到文本区域,javascript,jquery,textarea,Javascript,Jquery,Textarea,我正在尝试开发应用程序,但无法填写以下文本区域: var txt= window.localStorage.getItem("phrase"); var area=document.getElementById(window.localStorage.getItem("id_travail")); $(area).val(txt); 也许您需要告诉jQuery区域是一个ID,除非您在中定义它 window.localStorage.getItem(“id\u-travail”) 您的代码运行良
var txt= window.localStorage.getItem("phrase");
var area=document.getElementById(window.localStorage.getItem("id_travail"));
$(area).val(txt);
也许您需要告诉jQuery区域是一个ID,除非您在中定义它 window.localStorage.getItem(“id\u-travail”) 您的代码运行良好(这里是一个示例)。您的问题是:
window.localStorage.getItem(“id\u-travail”)
返回的值与页面上任何文本区域的id都不匹配document.getElementById
找不到textarea,因为它还没有作为DOM元素存在。尝试将代码包装在$(document).ready(function(){…})
中,以便代码仅在构建DOM后运行做一个jsfiddle…它变得很简单4ppl2debugworks对我来说很棒:。也许您没有将
localStorage['id\u-travail']
的值正确设置为文本区域的id?@MichaelBerkowski非常感谢:)
$("#"+area).val(txt)