简体   繁体   English

使用javascript动态添加选择框的选项值

[英]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;
        });
  1. Just Use .value and assign cur_year to it. 只需使用.value并为其指定cur_year

Working Fiddle 工作小提琴

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

相关问题 使用jquery在选择框中动态添加选项组和选项 - Dynamically add option group and option in select box using jquery 使用 jQuery/javaScript 从选择框中动态选择选项 - select dynamically option from select-box using jQuery/javaScript 如何使用Javascript通过选项的值在选择框中选择一个选项? - How to select an option in a select box by the value of the option using Javascript? 如何使用php或javascript单击添加按钮后动态添加选择框字段并将所选值存储在数据库中的所选框中 - How to add select box field dynamically after clicking add button by using php or javascript and store selected value in selected box in database Javascript动态添加选项值 - Javascript Dynamically Add Option Value 使用jquery或javascript在选择框中隐藏重复的选项文本值 - Hide repeated option text value in select box using jquery or javascript 使用JavaScript动态添加/删除选择框HTML表 - Dynamically Add/Remove select box HTML table using JavaScript javascript html选择动态添加optgroup和选项 - javascript html select add optgroup and option dynamically 动态添加另一个选择框选项的html select onClick - Dynamically add html select onClick of another select box option 如何使用javascript或jquery从选择框选项的第一个选项中清空值? - How to empty a value from the first option of a select box option using javascript or jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM