【问题标题】:onClick javascript changes are not reflected without reloading pageonClick javascript 更改不会在不重新加载页面的情况下反映出来
【发布时间】:2020-12-15 21:09:19
【问题描述】:

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

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

现在我的问题是 Amount 字段的 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;
}

【问题讨论】:

  • 欢迎来到 SO。请提供足够的 html 以根据 minimal reproducible example 重现您的问题
  • 你的锚标签在按钮内吗?我们可以看看你的 HTML 代码吗?
  • 它是一个 .cgi 文件。我添加了 perl 代码 sn-p,它为开始日期创建选项,月份和年份相同
  • 会有什么问题??同样适用于文本字段

标签: javascript html dom layout


【解决方案1】:

您需要循环选择选项以匹配设置索引的值。

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;

【讨论】:

  • 但是我们可以使用document.getElementById("lstStartDate").value = datealso来设置索引吗??
  • 不,value 不是 select 元素的属性。
  • 我认为它在 Internet Explorer 中确实有效,但在其他浏览器中不太可能。
  • 正如我所提到的,指针指向正确的索引,但它没有反映在页面中。意味着单击重置后,当我单击下拉菜单时,它选择了所需的索引值,但所选值未反映在页面中。在调试 JS 更改时正确发生。但我的问题是它没有反映。
猜你喜欢
  • 2020-10-04
  • 2013-07-07
  • 1970-01-01
  • 2016-10-28
  • 2020-04-20
  • 2013-06-21
  • 2012-05-26
  • 2020-12-29
相关资源
最近更新 更多