繁体   English   中英

如何访问HTML标记内的JavaScript变量

[英]How to access the javascript variable inside html tags

我想在html代码中添加javascript变量。 这是我的代码:

 <select id="currentrun" name="currentrun" >
        <option value=""><script>selectedrun</script></option>
 </select>

和我的js功能

 var currentrun="";
 var selectedrun="";
  function setCurrentRun()
  { 
    currentrun = document.getElementById("runlist");
    selectedrun = currentrun.options[currentrun.selectedIndex].value;       
   }

但这是行不通的。

谁能帮我做到这一点。

提前致谢。

您不必像那样运行脚本。

可以通过以下方式检索currentrun的值:

document.getElementById('currentrun').value

为了完整起见,如果您想在每次修改选择时触发JavaScript函数调用,请执行以下操作:

<select onchange="my_function();">
  <option>...</option>
</select>

似乎您正在尝试使用javascript设置option元素的文本。 这是一种使用元素id来获取它,然后将元素的text属性设置为javascript变量的方法:

<select id="currentrun" name="currentrun" >
    <option id="currentoption" value=""></option>
</select>

<script>
    document.getElementById("currentoption").text = selectedrun;     
</script>

确保在上述脚本之前放置任何声明和设置selectedrun变量的必要javascript。

暂无
暂无

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

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