簡體   English   中英

如何在選項標簽中顯示javascript變量

[英]How to display a javascript variable in option tag

我需要顯示一個在選項塊的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>        

運行時當前代碼生成的圖像

任何幫助都會非常感激,我對編寫網頁和使用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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM