1

選択した値と他の値を表示するjavascriptドロップダウンがありますが、選択した値はドロップダウンに2回表示され、1回だけ表示されるはずです。2013、2014.2015 の 3 つの値しかなく、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>
4

3 に答える 3

0

<option>タグを閉じていないようです。

また、上記のコメントで述べたように、タグを手動で書き出すよりもはるかに優れた方法がありますdocument.write。この方法で手動で HTML を書き出すと、エラーが発生しやすくなります。

于 2013-10-31T05:00:50.463 に答える
0

do-whileループと関係があると思います。do while ループは、while の条件をチェックする前にループの内容を 1 回実行するため、値は 2 回表示されます。

一方、ループを使用 whileすると、コンテンツを実行する前に最初に条件がチェックされ、条件の前に実行されません。

于 2013-10-31T05:04:53.960 に答える
0

ループに条件を配置して、選択した値をスキップする必要があることを知らせる必要があります。以下で説明します。

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);
于 2013-10-31T05:05:03.017 に答える