我需要显示一个在选项块的javascript脚本中声明的变量。将有一到两个选择块依赖于此块中选择的选项。
<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>
Image of what current code produces when run
任何帮助都将非常感激,我非常新编码网页和使用java脚本。对不起,如果这个问题已被提出并得到解答,我很抱歉。
答案 0 :(得分:0)
您可以动态添加选项。
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.