jQuery - 在其他地方使用 javascript 变量
我担心这个问题可能是非常新手级别的,但我只是一片空白。
在 $(document).ready 函数中,我有一些 DatePicker 代码...
$('#date-view1').datePicker({
selectWeek: true,
inline: true,
startDate: '01/01/1996'
}).bind('dateSelected', function (e, selectedDate, $td) {
$('#date1').val(selectedDate.asString());
var pfb = selectedDate.asString();
});
我正在努力解决的部分是
我想要的 var pfb = selectedDate.asString(); 在页面下方的另一个名为 showProjects()
的函数中使用变量 pfb
。
我该怎么做?我尝试在 $(document).ready 函数内部和外部声明变量,但没有成功。
谢谢
I fear this question may be extremely newbie level, but I am just drawing a blank.
Within the $(document).ready
function I have some DatePicker code...
$('#date-view1').datePicker({
selectWeek: true,
inline: true,
startDate: '01/01/1996'
}).bind('dateSelected', function (e, selectedDate, $td) {
$('#date1').val(selectedDate.asString());
var pfb = selectedDate.asString();
});
The part I am struggling with is the var pfb = selectedDate.asString();
I want to use the variable pfb
further down my page in a different function called showProjects()
.
How can I do this? I have tried declaring the variable inside and outside of the $(document).ready
function without luck.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在文档就绪块之前声明
var pfb
。这将使它在页面的其他地方可用。在准备好的文档中,您将设置一个已经声明的变量。Declare
var pfb
before your document ready block. That'll make it available elsewhere on the page. In the document ready you'll be SETTING an already DECLARED variable.在 Javascript 中,您可以使用全局变量来存储可从页面中的任何位置访问的值。有多种方法可以做到这一点
使用 window.pfb = selectedDate.asString(); 设置值
并稍后使用
window.pfb
在任何地方访问它In Javascript you can use global variables to store values which are accessible from anywhere in the page. Of the many ways of doing this is
setting the value using
window.pfb = selectedDate.asString();
and accessing it later anywhere with
window.pfb
我不确定这是否是一个问题区域,但我不会尝试在 onclick 事件中将 pfb 作为参数传递 - 我认为这可能会重新初始化 pfb,或创建一个新的 var。
如果您在全局范围内创建它(不理想但应该可以工作),那么您无论如何都不需要将 pfb 作为参数传递。
另外,最好不要将事件附加到这样的元素上。理想情况下 - jQuery 使这变得非常简单 - 您的 $(document).ready 中应该有这样的内容:
或者甚至可以将其缩短为,
如果您知道 showProjects() 是您想要进行的唯一调用。
I'm not sure if this is a problem area, but I wouldn't have tried passing pfb as a param in that onclick event - I think that may re-initialise pfb, or create a new var.
If you're creating it globally (not ideal but should work) then you shouldn't need to pass pfb as a param anyway.
Also, it's good practice not to attach events on the elements like that. Ideally - and jQuery makes this very easy - you should have something in your $(document).ready like this:
Or even shorten this to
if you know that showProjects() is the only call that you want to make.
如果您只删除单词
var
,它应该可以工作。声明变量而不使用
var
会使它们成为全局变量。正如丹·雷(Dan Ray)建议的那样,在就绪块之前声明它可能是更好的形式,但你说你对此感到困难?不知道你为什么会这么做。
It should work if you just drop the word
var
Declaring variables without
var
makes them global.It would probably be better form to declare it before the ready block as Dan Ray suggested, but you said you had a hard time with this? Not sure why you would.