下拉列表中的选定值在下拉列表中显示两次
Selected values in dropdown display two times in dropdown
我有一个javascript下拉列表,它显示所选值和其他值,但所选值在下拉列表中显示2次,它应该显示1次。请任何人帮助我纠正 problem.eg。如果只有 3 个值 2013, 2014.2015 我选择 2013 但它显示 2013 年选择的值 2013 2013 2014 2015
年<script type="text/javascript">
function autoYear() {
var time = new Date();
var year = time.getYear();
if (year < 1900) {
year = year + 1900;
}
var date = year - 1; /*change the '25' to the number of years in the past you want to show */
var future = year + 10; /*change the '10' to the number of years in the future you want to show */
document.writeln ("<form><select id='year_<?php echo $row->id ?>' name='year_<?php echo $row->id ?>' ><option value='"'"><?php echo $row->year; ?>");
do {
date++;
document.write ("<option value='"" +date+"'">" +date+ "");
}
while (date < future)
document.write ("</select></form>");
}
</script>
<script type="text/javascript">
autoYear();
</script>
看起来您没有关闭<option>
标签。
此外,如上面的评论中所述,有比手动写出带有document.write
标签更好的方法。以这种方式手动写出 HTML 时,您更容易产生错误。
我想这与do-while
循环有关。因为 do while 循环在检查 while 的条件之前执行一次循环的内容。因此,您的值将显示两次。
而如果您使用 while
循环,它将在执行内容之前首先检查条件,而不会在条件之前执行。
您需要在循环中放置一个条件,让它知道它需要跳过所选值,请允许我在下面演示:
var date = year - 1; /*change the '25' to the number of years in the past you want to show */
var future = year + 10; /*change the '10' to the number of years in the future you want to show */
document.writeln ("<form><select id='year_<?php echo $row->id ?>' name='year_<?php echo $row->id ?>' ><option value='"'"><?php echo $row->year; ?>");
do {
date++;
if (date != <?php echo $row->year; ?>) {
document.write ("<option value='"" +date+"'">" +date+ "");
}
}
while (date < future);
相关文章:
- 用每小时的差值填充数组/列表-从下拉列表中给定两个时间值
- 根据两个下拉列表的默认值禁用按钮
- 根据前两个下拉ID显示第三个下拉列表
- 使用两个下拉列表的值填充文本框
- 为什么jQuery下拉列表需要单击两次
- 需要在 html 中使用两个下拉列表来显示不同的 json 值
- 下拉列表多次保持上下滑动
- 两个下拉列表中的文本字段
- 多类别下拉列表,可在 JavaScript 中多次使用
- 如何在AngularJS中显示基于两个下拉列表的内容
- 将同一个元素追加到两个下拉列表中只会导致最后一个元素被追加
- jQuery验证插件-比较两个选择下拉列表的自定义方法
- 下拉列表的onChange()第二次不起作用
- javascript动态下拉列表-两种形式相互依赖
- Kendo Grid UI水印列或一列的两个下拉列表
- 下拉列表中的选定值在下拉列表中显示两次
- 使用 Ajax 一次填充两个下拉列表
- 单击两次图标不会关闭下拉列表
- 使用下拉列表一次选择一个图表
- 下拉列表中的两个不同值运行相同的函数,但只需要运行一次