[英]add option value of the select box dynamically using javascript
I want to populate the year in a select javascript. 我想在一个精选的javascript中填充年份。 I did the following code. 我做了以下代码。
<html>
<head>
<script type="text/javascript">
$(document).ready(function(){
var cur_year=new Date().getFullYear();
var obj=document.getElementById("yr");
alert(obj);
for (var i = 2000; i <= 2020; i++) {
opt = document.createElement("option");
opt.appendChild(document.createTextNode(value));
opt.selected = selected;
opt.value = i;
opt.text=i;
obj.appendChild(opt);
}
});
</script>
</head>
<body>
<select id="yr">
<option>year</option>
</select>
</body>
</html>
I don't know what is wrong in this. 我不知道这有什么问题。 I want to populate the year and want to select the current year in the select box when the user opens the browser. 我想填充年份,并希望在用户打开浏览器时在选择框中选择当前年份。 Any one can help? 任何人都可以帮忙吗? please! 请!
$(document).ready( function() {
var cur_year=new Date().getFullYear();
var obj=document.getElementById("yr");
for (var i = 2000; i <= 2020; i++) {
opt = document.createElement("option");
opt.value = i;
opt.text=i;
obj.appendChild(opt);
}
document.getElementById("yr").value = cur_year;
});
.value
and assign cur_year
to it. 只需使用.value
并为其指定cur_year
。 <?php
$current_year = date('Y');
$end_year = date('Y', strtotime('+10 years'));
?>
<label>Year</label><select name="year">
<?php
for($i=$current_year; $i<=$end_year; $i++)
{
?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php
}
?>
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.