我遇到一个问题,我似乎无法将变量“ notes”传递给表单的占位符。以下代码提供更多信息:
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);
}
});
});
占位符代码有效...如果我使用
$(this).attr("placeholder","test");
它可以正常工作,但如果我尝试使用var注释,则不会。
设置var注释也可以。如果取消对document.write("notes")
的注释,则将获得期望的值。
我尝试过:
我最初以Var notes = data;
开始,但根据另一个问题的建议将其移至window.notes
,以使其具有全球性。不工作。我还尝试通过在函数范围之外将其设置为空来“强制”变量为全局变量,但这还是行不通的。我缺少什么吗?
谢谢!
答案 0 :(得分:1)
对不起,我没有什么可评论的。我只是想说document.ready函数在页面加载开始时就在加载时起作用,而request.done或request.fail在它加载后才起作用,因此最初加载window.notes时是不确定的。 而且当您使用代码document.write(notes)时,它便已定义,因此没有错误。