Javascript 在dropdownlist中获取不可见的值
我希望使用javascript在下拉列表中有多个值,这样就不需要在选择标记的选项中写入太多值,所以我使用下面的代码来实现这一点,但当在internet explorer中打开时,它会显示不可见的值,这意味着分配的空间是可见的,而不是内容,在chrome&firefox中,它什么也不显示Javascript 在dropdownlist中获取不可见的值,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我希望使用javascript在下拉列表中有多个值,这样就不需要在选择标记的选项中写入太多值,所以我使用下面的代码来实现这一点,但当在internet explorer中打开时,它会显示不可见的值,这意味着分配的空间是可见的,而不是内容,在chrome&firefox中,它什么也不显示 <script> function Time(){ selectHr(); selectMin(); selectSec(); }
<script>
function Time(){
selectHr();
selectMin();
selectSec();
}
function selectHr()
{
for(var x=1;x<=24;x++)
{
var option = document.createElement("option");
option.text = x;
option.value=x;
document.getElementById('dateTime_selHr').add(option);
}
}
function selectMin()
{
for(var x=1;x<=60;x++)
{
var option = document.createElement("option");
option.text = x;
option.value=x;
document.getElementById('dateTime_selMin').add(option);
}
}
function selectSec()
{
for(var x=1;x<=60;x++)
{
var option = document.createElement("option");
option.text = x;
option.value=x;
document.getElementById('dateTime_selSec').add(option);
}
}
</script>
</head>
<body onload="Time()">
<table class="table" id="dateTime_tbl">
<tr>
<td>
Camera
</td>
<td colspan="2">
<input type="text" name="camera" />
</td>
</tr>
<tr>
<td>
Time In Camera Date
</td>
<td>
Date <input type="text" name="date"/>
Time <input type="text" name="time" />
</td>
</tr>
<tr>
<td> Set Time</td>
<td>
<input type="radio" /> Set manually
</td>
</tr>
<tr>
<td></td>
<td>Date <input type="text" name="txtdate" />
Time
hr : <span> <select name="hr" id="dateTime_selHr"> </select> </span>
min : <span> <select name="min" id="dateTime_selMin" > </select> </span>
sec : <span> <select name="sec" id="dateTime_selSec" > </select> </span>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="radio" name="syncTime" />Synchronize with computer time
</td>
</tr>
<tr>
<td></td>
<td>
Date <input type="text" />
Time <input type="text" />
</td>
</tr>
<tr>
<td></td>
<td>
<input type="radio" /> Synchronize with SNTP server
</td>
</tr>
<tr>
<td></td>
<td>
SNTP Server <input type="text" style="width:200px"/>
</td>
</tr>
<tr>
<td></td>
<td>
Time Zone <span> <select name="timeZone">
<option></option>
</select></span>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="checkbox" /> Automatically adjust for daylight saving time changes
</td>
</tr>
<tr>
<td>
Date Format <span><select>
<option>-select-</option>
<option>YYYY/MM/DD</option>
<option>MM/DD/YYYY</option>
<option>DD/MM/YYYY</option>
</select></span>
</td>
<td>
Time Format <span> <select>
<option>-select-</option>
<option>12 hour format</option>
<option>24 hour format</option>
<option>DD/MM/YYYY</option>
</select></span>
</td>
</tr>
<tr>
<td>
Date Position <span><select>
<option>-select-</option>
<option>Bottom Left</option>
<option>Bottom Right</option>
</select></span>
</td>
<td>
Time Position <span> <select>
<option>-select-</option>
<option>Bottom Left</option>
<option>Bottom Right</option>
</select></span>
</td>
</tr>
</table>
<center>
<div id="dateTime_actionBtnsDiv">
<button type="button" name="ok" style="width:80px" id="dtTime_btns">OK</button>
<button type="button" name="cancel" style="width:80px" id="dtTime_btns">CANCEL</button>
</div>
</center>
</body>
功能时间(){
选择HR();
selectMin();
选择sec();
}
函数selectHr()
{
对于(var x=1;x,可以改用add()函数
function selectFunction()
{
for(var x=1;x<=5;x++)
{
var option = document.createElement("option");
option.text = x;
option.value=x;
document.getElementById('selectId').add(option);
}
}
function selectFunction()
{
对于(var x=1;x而言,该方法效果良好,并在IE中进行了测试
<script>
function selectFunction()
{
var selector = document.getElementById('selectId');
for(var x=1;x<=5;x++)
{
var opt = document.createElement('option');
opt.value = x;
opt.innerHTML = x;
selector.appendChild(opt);
}
}
</script>
<body onload="selectFunction()">
<select id="selectId"></select>
</body>
函数selectFunction()
{
var selector=document.getElementById('selectId');
对于(var x=1;x这是Microsoft注册的错误
因此,我建议您不要使用您的代码
var option = document.createElement("option");
option.text = x;
option.value=x;
document.getElementById('selectId').add(option);
你能告诉我们你所说的不可见值是什么意思吗
?我已经测试了你的代码。它似乎工作正常。你得到了什么错误?@SpiderCode在IE 9中为我显示了错误操作状态:请检查答案,如果上面的解决方案能够解决你的问题