Javascript 检索JS变量
在上面的代码中,我使用变量selectedLob来存储从下拉列表中选择的值 如何从函数外部检索该值 也就是说,这个函数存储在file1.js中——我如何从另一个文件夹中的file2.js访问这个变量Javascript 检索JS变量,javascript,jquery,Javascript,Jquery,在上面的代码中,我使用变量selectedLob来存储从下拉列表中选择的值 如何从函数外部检索该值 也就是说,这个函数存储在file1.js中——我如何从另一个文件夹中的file2.js访问这个变量 谢谢var selectedLob是$('#lobSelect')本地的。更改(函数(){函数无法在外部访问 解决方案 $('#lobSelect').change(function () { var selectedLob = $('#mainWrapper').find('select[
谢谢
var selectedLob
是$('#lobSelect')本地的。更改(函数(){
函数无法在外部访问
解决方案
$('#lobSelect').change(function () {
var selectedLob = $('#mainWrapper').find('select[name="lob-select"] option:selected').val();
console.log(selectedLob); //prints to the console
});
console.log(selectedLob); //not available here
读取使用全局变量
var selectedLob =''; //declare variable outside so it accessible
$('#lobSelect').change(function () {
//assign value here
selectedLob = $('#mainWrapper').find('select[name="lob-select"] option:selected').val();
console.log(selectedLob); //prints to the console
});
console.log(selectedLob);
//it doesn't get the value as it runs before the change event fires so you get undefined value
当您声明一个变量时,您会使它成为它所声明的函数范围的局部变量 在函数外部声明它:
var selectedLob; // use as a global variable to access outside of the function
$('#lobSelect').change(function () {
selectedLob = $('#mainWrapper').find('select[name="lob-select"] option:selected').val();
console.log(selectedLob); //prints to the console
});
if(selectedLob!=undefined){
console.log(selectedLob); //available here
}
不要在里面重新声明:
var selectedLob;
这里仍然没有定义:
$('#lobSelect').change(function () {
selectedLob = $('#mainWrapper').find('select[name="lob-select"] option:selected').val();
console.log(selectedLob); //prints to the console
});
因为只有在触发更改事件后,它才会获得分配给它的值,但在该事件发生后,它将在更大范围内被访问。您可以使用jQuery函数存储在$(“#lobSelect”)
然后像这样检索:
$('#lobSelect').data("yourKey", yourVariable)
// use the same app namespace or create it
// if not exist yet (in case when file2.js was loaded before file1.js)
var app = app || {};
$(function () {
// app.selectedLob is available here
});
或者,如果您希望在之后立即使用该变量,请按照建议在函数外部声明该变量(数据将存储更长的时间):
还可以使用以下方法在函数中设置全局变量
var selectedLob;
$('#lobSelect').change(function () {
selectedLob = $('#mainWrapper').find('select[name="lob-select"] option:selected').val();
console.log(selectedLob); //prints to the console
});
console.log(selectedLob);
//accessible here,
//maybe not elsewhere in your code (depending on your code structure)
然后在任何地方访问它。例如:
window["variableName"] = "some value";
您可以在任务中使用全局变量,但这将导致全局名称空间受到污染 好的做法是使用应用程序名称空间而不是全局名称空间 您的file1.js
console.log(window["variableName"]);
您的file2.js
// your app namespace
var app = app || {};
// on document ready wrapper
$(function () {
$('#lobSelect').change(function () {
// initialize your variable at app namespace
app.selectedLob = $('#mainWrapper').find('select[name="lob-select"] option:selected').val();
});
});
这里的第一个问题是,您使用的
selectedLob
变量超出了它定义的范围,因此您必须将它的声明向上移动一级,以便它也可以在函数之外使用(或者更好的是,您应该重新构造代码,这样就不需要这种类型)
第二个问题是在更改事件处理程序中声明selectedLob
变量,并希望立即定义它。要解决此问题(如果您在这里使用JQuery),可以在声明更改处理程序函数后立即调用该函数以开始变量初始化
因此,总结如下:
$('#lobSelect').data("yourKey", yourVariable)
// use the same app namespace or create it
// if not exist yet (in case when file2.js was loaded before file1.js)
var app = app || {};
$(function () {
// app.selectedLob is available here
});
最后我要说的是,你真的应该避免这样的事情,试着重新构造你的JS代码,在DOM准备好后,你可以在一个地方初始化你需要的所有东西,然后用某种init函数启动你的应用程序,传递所有需要的东西或类似的东西。编写这样的代码很快就会导致一大堆乱七八糟的东西:)将该变量分配到全局范围。请参考下面ManiShankar的答案。很抱歉,我尝试了这个方法,但仍然不起作用。@Downvoter:您能告诉我您投票的原因吗?以便我和观众知道错误是什么。@ManiShankar确定。
selectedLob
在更改
事件之后才设置。在您的示例,selectedLob
仍然是undefined@RGraham现在你已经看到了我编辑的帖子+1,这是唯一一个提到selectedLob
只会在change
事件后填充的答案。+1-在这个帖子上有@RGraham。尝试了上面的方法-但即使在change事件之后仍然没有定义。@Spdexter,在更改事件之后,您如何记录它?var selectedLob='';$(function(){$('#lobSelect')。更改(function(){selectedLob=$('#mainWrapper')。查找('select[name=“lob select”]option:selected')。val();});});console.log(selectedLob);downvoter可以评论吗?我可以。更改存储机制不会影响问题。问题在于访问变量的位置,而不仅仅是作用域的问题。您完整阅读了问题吗?Spdexter还希望从另一个文件访问此变量,可能不在同一作用域,我看不出其他人如何回答这一点您是是的。错过了最后一行,只是看了一下例子(很明显,这并不能代表问题!)。
var selectedLob =''; //declare variable outside change handler so it accessible
$('#lobSelect').change(function () {
//assign value here
selectedLob = $('#mainWrapper').find('select[name="lob-select"] option:selected').val();
console.log(selectedLob); //prints to the console
}).change(); // This will call your change event handler and hopefully init selectedLob
// Now selectedLob should be have value of selected option (if your DOM was ready...)
console.log(selectedLob);