2016-10-21 45 views
0

我想问一下如何改变月份的格式selectmonth到“九”,而不是09如何从下拉列表中格式化日期?

function myFunction() { 
var d = new Date(); 
var month = new Array(); 
month[0] = "January"; 
month[1] = "February"; 
month[2] = "March"; 
month[3] = "April"; 
month[4] = "May"; 
month[5] = "June"; 
month[6] = "July"; 
month[7] = "August"; 
month[8] = "September"; 
month[9] = "October"; 
month[10] = "November"; 
month[11] = "December"; 
var x = month[d.document.getElementById("selectmonth").value]; 
var y = document.getElementById("selectyear").value; 
document.getElementById("demo").innerHTML = "Quick summary for " + x + y ; 
} 
+0

你的意思是你想要的下拉本身显示“一月”,“二月”,等等,或者你只是想更改选定显示的函数内的值? – nnnnnn

+0

@nnnnnn我绝对想要改变显示的功能内的选定值 – hihi

+0

@nnnnnn我的下拉本身ady显示“一月”,“二月”等 – hihi

回答

0

只需创建一个包含所有月份并通过索引引用数组的数组。

function myFunction() { 
 

 
    var month = ["January", 
 
       "February", 
 
       "March", 
 
       "April", 
 
       "May", 
 
       "June", 
 
       "July", 
 
       "August", 
 
       "September", 
 
       "October", 
 
       "November", 
 
       "December"] 
 
    
 
    var x = document.getElementById("selectmonth").value; 
 
    var y = document.getElementById("selectyear").value; 
 
    
 
    document.getElementById("demo").innerHTML = "Quick summary for " + month[x - 1] + y ; 
 
}
<input type="text" id="selectmonth" value="9"/> 
 
<input type="text" id="selectyear" value="2016"/> 
 
<div id="demo"></div> 
 
<input type="button" onclick="myFunction()" value="TestMe" />

0
function myFunction() { 
var month = new Array(); 
month[0] = "January"; 
month[1] = "February"; 
month[2] = "March"; 
month[3] = "April"; 
month[4] = "May"; 
month[5] = "June"; 
month[6] = "July"; 
month[7] = "August"; 
month[8] = "September"; 
month[9] = "October"; 
month[10] = "November"; 
month[11] = "December"; 
var x = month[parseInt(document.getElementById("selectmonth").value,10)]; 
var y = document.getElementById("selectyear").value; 
document.getElementById("demo").innerHTML = "Quick summary for " + x + y ; 
} 
+0

而不是getMonth我从下拉列表中获取月份 – hihi

+0

您必须parseInt您的月份值,因为它的值包含'09'。 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/parseInt –

1

我觉得你应该自己的价值 从HTML标签selectmonth改变。

window.onload = function(){ 
 
    document.getElementById("select").onchange = function() { 
 
     var get={}; 
 
     get.x=function() { 
 
      return document.getElementById("selectmonth").value; 
 
     } 
 
     get.y=function() { 
 
      return document.getElementById("selectyear").value; 
 
     } 
 
     document.getElementById("demo").innerHTML = 
 
       "Quick summary for " + get.x() + get.y(); 
 
    } 
 
}
<div id="select"> 
 
    <select id="selectmonth"> 
 
     <option name="month" value="">Select...</option> 
 
     <option name="month" value="January">January</option> 
 
     <option name="month" value="February">February</option> 
 
     <option name="month" value="March">March</option> 
 
     <option name="month" value="April">April</option> 
 
     <option name="month" value="May">May</option> 
 
     <option name="month" value="June">June</option> 
 
     <option name="month" value="July">July</option> 
 
     <option name="month" value="August">August</option> 
 
     <option name="month" value="September">September</option> 
 
     <option name="month" value="October">October</option> 
 
     <option name="month" value="November">November</option> 
 
     <option name="month" value="December">December</option> 
 
    </select> 
 
    <select id="selectyear"> 
 
     <option name="year" value="2016" selected>2016</option> 
 
     <option name="year" value="2017">2017</option> 
 
     <option name="year" value="2018">2018</option> 
 
    </select> 
 
</div> 
 
<div id="demo"></div>

希望能帮助你。

1

你在你的选择选项显示的月份名称评论说,这样推测,这意味着你的选择是这样的:

<option value="09">September</option> 

如果是这样,那么你可以得到所选项目的值和显示名称,如下所示:

var monthDropdown = document.getElementById("selectmonth"); 
var x = monthDropdown.value 
var monthName = monthDropdown.options[monthDropdown.selectedIndex].textContent; 

在背景:

function myFunction() { 
 
    var monthDropdown = document.getElementById("selectmonth"); 
 
    var x = monthDropdown.value 
 
    var monthName = monthDropdown.options[monthDropdown.selectedIndex].textContent; 
 
    var y = document.getElementById("selectyear").value; 
 
    document.getElementById("demo").innerHTML = "Quick summary for " + monthName + " " + y + " (the month number was " + x + ")"; 
 
} 
 
document.querySelector("button").addEventListener("click", myFunction);
<select id="selectmonth"> 
 
    <option value="01">January</option> 
 
    <option value="02">February</option> 
 
    <option value="03">March</option> 
 
    <option value="04">April</option> 
 
    <option value="05">May</option> 
 
    <option value="06">June</option> 
 
    <option value="07">July</option> 
 
    <option value="08">August</option> 
 
    <option value="09">September</option> 
 
    <option value="10">October</option> 
 
    <option value="11">November</option> 
 
    <option value="12">December</option> 
 
</select> 
 
<input id="selectyear" value="2016"> 
 
<button>Test</button> 
 
<div id="demo"></div>