如何在选项标记中显示javascript变量
我需要在选项块中显示javascript脚本中声明的变量。将有一到两个以上的选择块依赖于此块中选择的选项如何在选项标记中显示javascript变量,javascript,html,dom,Javascript,Html,Dom,我需要在选项块中显示javascript脚本中声明的变量。将有一到两个以上的选择块依赖于此块中选择的选项 <select name="expiration_year" id="expiration_year" onchange="populate(this.id,'expiration_month','expiration_day')"> <script> //variable declaration in JavaScript var
<select name="expiration_year" id="expiration_year" onchange="populate(this.id,'expiration_month','expiration_day')">
<script>
//variable declaration in JavaScript
var year = new Date().getFullYear();
</script>
//I want to display the variable stored in "year" where it is marked in the
//following code
//this <option value=year> is using the variable correctly, the second
//half of the line isnt using the variable (which should have a value
//equaling the current year)
//instead it simply says year
<select>
<option value=year>year</option>
<option value=year+1>year+1</option>
</select>
//JavaScript中的变量声明
变量年份=新日期().getFullYear();
//我想显示存储在“年”中的变量,该变量在
//以下代码
//这是正确使用变量,第二个
//行的一半没有使用变量(应该有一个值
//与本年度持平)
//相反,它只是说年
年
年+1
任何帮助都将不胜感激,我对网页编码和使用java脚本非常陌生。如果这个问题已经被询问和回答,我很抱歉。您可以动态添加选项 e、 g
您可以动态添加选项 e、 g
谢谢你的回复!我的最终目标是,在页面上总共有三个选择。依赖顺序为年==>月==>日。以年、月、日为选择的名称。我已经更新了问题,以反映将有多个选择。再次感谢!谢谢你的回复!我的最终目标是,在页面上总共有三个选择。依赖顺序为年==>月==>日。以年、月、日为选择的名称。我已经更新了问题,以反映将有多个选择。再次感谢!
// This assumes that this is the only select element on the page.
// Also, this would have to run in a script block
// after the select element is added to the page.
var select = document.querySelector('select');
select.options.add(new Option(year));
select.options.add(new Option(year + 1));
// etc.