【问题标题】:Set variable and use it设置变量并使用它
【发布时间】:2022-08-14 17:18:57
【问题描述】:

我无法设置变量:

<input type=\"text\" id=\"routeprofile\"><button onclick=\"profileFunction()\">Choisir</button><p id=\"profile\"></p>

的JavaScript:

var routingprofile;

function profileFunction() {
        const routingprofile = document.getElementById(profile);



    alert(\"Value after trigger:\" + routingprofile)
} ;

结果是:空。

我希望它在使用时全局可用:

L.Routing.control({
   
    router: osrRouter,
     position: \'topleft\',
     collapsible: true,
profile: routingprofile,
    geocoder: L.Control.Geocoder.nominatim({}) 

  //  }),
   
}).addTo(map);
  • const routingprofile = document.getElementById(profile); 你的意思是在这里使用字符串吗?还是profile 在某处定义?如果是前者,那么getElementById(\"profile\");
  • document.getElementById(\'profile\'); // &lt;-- missing quotes in argument.
  • @pilchard 我想使用 id=\"routeprofile\" 文本框的内容,它是一个文本字符串

标签: javascript


【解决方案1】:

在 get element by id 处添加引号:

来自: const routingprofile = document.getElementById(profile); to: const routingprofile = document.getElementById("profile");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 2011-02-13
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多