我有一些javascript代码的问题,我有我的解决方案在PHP但我需要它在JavaScript,谁能给我一些提示如何翻译?
<select>
<?php
$current_month = date("n");
?>
<option value='1' <?php if($current_month == 1) { echo "selected";} ?>>January</option>
<option value='2' <?php if($current_month == 2) { echo "selected";} ?>>February</option>
<option value='3' <?php if($current_month == 3) { echo "selected";} ?>>Mars</option>
<option value='4' <?php if($current_month == 4) { echo "selected";} ?>>April</option>
<option value='5' <?php if($current_month == 5) { echo "selected";} ?>>May</option>
<option value='6' <?php if($current_month == 6) { echo "selected";} ?>>June</option>
<option value='7' <?php if($current_month == 7) { echo "selected";} ?>>July</option>
<option value='8' <?php if($current_month == 8) { echo "selected";} ?>>August</option>
<option value='9' <?php if($current_month == 9) { echo "selected";} ?>>September</option>
<option value='10' <?php if($current_month == 10) { echo "selected";} ?>>October</option>
<option value='11' <?php if($current_month == 11) { echo "selected";} ?>>November</option>
<option value='12' <?php if($current_month == 12) { echo "selected";} ?>>December</option>
</select>
答案 0 :(得分:0)
您只需设置select元素的值即可使用Javascript更改所选选项。 (请注意,您必须为我的示例提供一个ID才能正常工作。)
var currentMonth = new Date().getMonth() + 1;
document.getElementById('months').value = currentMonth;