不重新加载页面不会反映onClick javascript更改

时间:2020-08-27 02:30:07

标签: javascript html dom layout

我有一个<select> <option></option </select>来表示停机报告的开始日期和结束日期,分别称为“停机开始数据”和“停机结束日期”。另外,我有一个文本字段可为称为Amount的停机报告输入数值。 注意:选项是由Perl脚本创建的。

在这里,对于文本字段以及开始日期和结束日期字段,我都有单独的resetclear锚标记。当单击reset作为开始日期或结束日期时,会将当前日期设置为相应的字段(开始或结束),对于“金额”字段将其设置为值150。单击clear时,将清除相应的字段。

现在,我的问题是“金额”字段的restclear工作正常。而且对于开始和结束日期,指针都指向选项中的所需索引(例如,明确表示它指向空选项,并将其重置为指向当前日期,月份,年份),但这没有反映在我的网页中无需重新加载页面(但这也不适用于chrome)。

我想让它像“金额”字段一样工作。

/*
Start Date snippet is the same for month and year
*/
<lable>Outage Start Date</lable>

<select>
for(sort keys $startDateSet) {
  if($_ eq lstStartDate) {
       $sel = "selected";
   }
   else {
       $sel = "";
   }
print "<option". $sel. "value=".$startDateSet[$_]."</option>
  }
</select>

<a id="lstStartDate" href="javascript:clearOutageStartDate();">clear</a>
<a id="lstStartDate" href="javascript:restOutageStartDate();">reset</a>

/* 
Same for the end date 
*/
function clearOutageStartDate() {
    document.outage_report.lstStartDate.selectedIndex = -1;
    document.outage_report.lstStartMonth.selectedIndex = -1;
    document.outage_report.lstStartYear.selectedIndex = -1;
}
/*
Same for the end date
*/
function resetOutageStartDate(month, date, year) {
    document.outage_report.lstStartDate.value = date;
    document.outage_report.lstStartMonth.value = month;
    document.outage_report.lstStartYear.value = year;
}

1 个答案:

答案 0 :(得分:0)

您需要遍历选择选项以匹配该值以设置索引。

var el = document.getElementById("lstStartYear");
for (i=0;i<el.size;i++) {
 if (el.option[i].value==year) { el.selectedIndex=i; }}

要清除,您应该使用

document.getElementById("lstStartYear").selectedIndex = -1;