繁体   English   中英

如何从 function JavaScript 内部获取全局变量?

[英]How to get global variable from inside function JavaScript?

When I am calling function name in the below, I am getting lots of errors like undefined but the function is working prefect, but when I am trying to callback global variable from function or function name getting useless errors. 问题是什么?。


    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>

考虑将其更改为像下面的示例一样工作。

在 function 中返回所需的结果,而不是尝试在 function 中设置它,然后将其设置为如下所示的函数返回值。

祝你好运!

 var quoteTex = ""; function wms() { return text = "test"; } console.log("1. " + quoteTex); quoteTex = wms(); console.log("2. " + quoteTex);

您的左括号和右括号不匹配。 您在 if-else 块之前开始了一个块代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM