检索JS变量
Retrieving JS variable
$('#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
在上面的代码中,我使用变量selectedLob来存储从下拉菜单中选择的值。
如何从函数外部检索该值?
也说这个函数存储在file1.js中-我如何从另一个文件夹的file2.js访问这个变量?
谢谢
当你声明一个变量时,你使它局部于函数的作用域。
在函数外声明:
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); //not available here
,因为在change事件触发之前,它不会被赋值,但在事件发生后,它将在更广泛的范围内被访问。
您可以为您的任务使用全局变量,但是这会导致污染全局命名空间。
最好的做法是使用应用程序命名空间而不是全局命名空间
你file1.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();
});
});
你file2.js // 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 () {
的本地函数,不能在外部访问
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
Read什么是JavaScript变量的作用域?
您还可以使用以下方法在函数内部设置全局变量
window["variableName"] = "some value";
然后在任何地方访问它。例如:
console.log(window["variableName"]);
可以使用jQuery。$('#lobSelect')
$('#lobSelect').data("yourKey", yourVariable)
并像这样检索:
var someVariable = $('#lobSelect').data("yourKey")
或者按照建议在函数外部声明变量,如果您想在之后使用它(数据将存储更长的时间):
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)
你在这里遇到的第一个问题是你在它定义的范围之外使用selectedLob
变量,所以你必须将它的声明向上移动一级,这样它就可以在函数之外使用(或者更好的是你应该重构你的代码,所以这种类型变得不必要)。
第二个问题是您在更改事件处理程序中声明selectedLob
变量,并期望立即定义它。为了解决这个问题(如果你在这里使用JQuery),你可以在你声明它之后调用你的change handler函数来初始化你的变量。
所以总结起来像这样:
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);
最后我想说的是,你应该尽量避免这样的事情,试着重构JS代码让你在DOM准备好后在一个地方初始化所有你需要的东西然后用一些init函数来启动你的应用传递所有它需要的东西。像这样写代码很快就会导致一大堆混乱:)
使用全局变量
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
}
- 等待函数完成,然后将返回值分配给变量JS
- 节点中的变量.js JavaScript 不会改变
- 如何在 Node 中访问模块全局变量.js例如浏览器中的 window.variable
- 在角度中使用父范围变量.js
- 在重新声明变量 (JS) 时遇到了问题
- 在主干中使用全局变量.js .最佳实践
- 在节点中使用变量.js mysql-node 查询
- 如果变量 JS 太高,则启动/停止反转
- 变量 Js 在 PHP 代码中不起作用
- 无法使用车把正确在我的“模板”中显示变量.js
- 引导程序中的全局变量.js在 Firefox for Android 扩展中
- Webstorm 中的“未解析变量 JS”
- 变量JS的子字符串值
- 动态更新变量JS
- 不能用一个主变量访问单个变量[jS]
- 创建全局变量js
- 当不同的函数需要从数据库调用数据时,如何避免使用全局变量.(JS)
- 变量JS中的变量
- 本地存储变量js
- 为循环追加变量js