gpt4 book ai didi

javascript - 如何在选项标签中显示 JavaScript 变量

转载 作者:行者123 更新时间:2023-11-28 05:52:32 25 4
gpt4 key购买 nike

我需要在选项 block 中显示在 javascript 脚本中声明的变量。还会有一到两个选择 block ,具体取决于在此 block 中选择的选项。

    <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 脚本非常陌生。如果这个问题已经被提出并得到回答,我很抱歉。

最佳答案

您可以动态添加选项。

例如

// 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.

关于javascript - 如何在选项标签中显示 JavaScript 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37977681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com