javascript - 如何从函数 JavaScript 中获取全局变量?
问题描述
当我在下面调用函数名时,我收到了很多错误,例如未定义但该函数运行良好,但是当我尝试从函数或函数名回调全局变量时出现无用的错误。问题是什么?。
var quoteTex = "";
function wms() {
var location = document.getElementsByName("myInput")[0]
var gwc = location.options[location.selectedIndex].value;
var layername = location.options[location.selectedIndex].text;
var url_geoserver = "http://localhost/geoserver/wms
if (gwc == 2913) {
var url = L.tileLayer.wms(url_geoserver, {
layers: 'ap_cadal:bagfinal',
format: 'image/png',
version: '1.1.0',
transparent: true,
maxZoom: 28
})
} else if (gwc == 2912) {
var url = L.tileLayer.wms(url_geoserver, {
layers: 'ap_cadas:basw',
format: 'image/png',
version: '1.1.0',
transparent: true,
maxZoom: 28
})
}
var te = JSON.stringify(url)
var myjson_p = JSON.parse(te)
var final = myjson_p["options"]["layers"]
quoteText = `${final}`
map.addLayer(url);
}
wms();
console.log(quoteTex)
HTML
<select name="myInput" id="choice1" onchange="wmslayers(this);">
<option value="" disabled selected>Please select </option></li>
<!-- Pati -->
<li><option value="2912">Baswari</option></li>
<li><option value="2913">Bagjiwala</option></li>
</select>
解决方案
考虑将其更改为像下面的示例一样工作。
在函数中返回所需的结果,而不是尝试在函数中设置它,然后将其设置为 = 函数返回值,如下所示。
祝你好运!
var quoteTex = "";
function wms() {
return text = "test";
}
console.log("1. " + quoteTex);
quoteTex = wms();
console.log("2. " + quoteTex);
推荐阅读
- angular - ngbDatepicker 防止 startDate 大于 endDate
- sql - Postgres 插入问题:“产品 1”列不存在。Postgres插入问题
- c# - MongoDB C# 驱动程序投影和序列化
- javascript - 如何将参数传递给 PostCSS 配置文件函数
- typescript - 打字稿:过滤包含另一个数组的每个元素的数组
- android - 约束布局中的 ViewPager 选项卡式活动在底部被剪切以用于 recyclerview
- sql - 多个表的一次查询中的多个完全连接
- apache-spark - 当 spark3.0 读取 spark2.2 写入的 orc 表并应用列修剪时读取所有列
- python-3.x - In pandas dataframe, how to make one column act on all the others?
- javascript - 如何获得最近的第一篇文章和第二篇……将它们放在博客模板中的幻灯片中