我有一个ASP下拉列表,我需要加载1到20的数字,默认选择1。如何用javascript做到这一点?我有一个示例代码但下载没有加载。我错过了什么吗?
<script>
function quantitydropdown() {
var ddl = document.getElementById('quantitydropdownid').getElementsByTagName("select")[0];
for (var i = 1; i <= 100; i++) {
var theOption = new Option;
theOption.text = i;
theOption.value = i;
ddl.options[i] = theOption;
}
}
</script>
<select id="quantitydropdownid" onchange="javascript:quantitydropdown();" runat="server" style="width: 200px;"></select>
答案 0 :(得分:1)
因此,当文档准备就绪时,我们会填充下拉列表:
// Set up event handler for when document is ready
window.addEventListener("DOMContentLoaded", function(){
// Get reference to drop down
var ddl = document.getElementById('quantitydropdownid');
for (var i = 1; i < 21; i++) {
var theOption = document.createElement("option");
theOption.text = i;
theOption.value = i;
// If it is the first option, make it be selected
i === 1 ? theOption.selected = "selected" : "";
ddl.options[i] = theOption;
}
});
&#13;
#quantitydropdownid { width:200px; }
&#13;
<select id="quantitydropdownid" runat="server"></select>
&#13;
答案 1 :(得分:0)
Please try with this code
-----------------------------------------
JS Code
----------------
$(document).ready(function(){
quantitydropdown();
})
function quantitydropdown()
{
for (var i = 1; i <= 20; i++)
{
$("#quantitydropdownid").append( $("<option></option>")
.attr("value", i)
.text(i)
);
}
}
Css Code
-----------
#quantitydropdownid { width:200px; }
HTML Code
-----------
<select id="quantitydropdownid" runat="server"></select>