0
<script>
var data = prompt("What year would you like the data for?");
if (data == 1901) {
    alert("26 thousand per million for males and 23 for females ");
} else if (data == 1910) {
    alert("23 thousand per million for males and 18 for females");
} else if (data == 1920) {
    alert("19 thousand per million for males and 15 for females");
} else if (data == 1930) {
    alert("17 thousand per million for males and 14.5 for females");
} else if (data == 1940) {
    alert("20 thousand per million for males and 15 for females");
} else if (data == 1950) {
    alert("15 thousand per million for males and 11 for females");
} else if (data == 1960) {
    alert("14.5 thousand per million for males and 9.5 for females");
} else if (data == 1970) {
    alert("14 thousand per million for males and 8 for females");
} else if (data == 1980) {
    alert("13 thousand per million for males and 5 for females");
} else if (data == 1990) {
    alert("10 thousand per million for males and 6 for females");
} else if (data == 2000) {
    alert("7 thousand per million for males and 5.2 for females");
} else if (data == 2010) {
    alert("7 thousand per million for males and 4.9 for females");

}else alert ("please enter a valid year from the chart.");

</script>

这是我的 javascript,它显然会打开一个提示并请求一个日期,但我希望这个提示在单击按钮时变为活动状态

获取日期 [CLICK HERE] -----> “您想要哪一年的数据?”

我该怎么做。这个按钮也需要进入 html。

4

2 回答 2

2

我可以建议一些清理工作吗?

var reportForYear = function(year) {
    var n = {
        1901: [26, 23],
        1910: [23, 18],
        1920: [19, 15],
        ⋮
    }[year];
    return !n ? "please enter a valid year from the chart."
        : n[0] + " thousand per million for males and " + n[1] + " for females";
};

var promptForYearAndReport = function() {
    var year = prompt("What year would you like the data for?");
    alert(reportForYear(year));
};

然后,在按钮的 onclick 上,调用promptForYearAndReport().

于 2013-10-09T21:59:44.707 回答
1

尝试这个。您的逻辑现在位于一个函数中,该函数在按钮上触发 click 事件时运行。

<script>
function yearData(){
  var data = prompt("What year would you like the data for?");
  if (data == 1901) {
    alert("26 thousand per million for males and 23 for females ");
  } else if (data == 1910) {
    alert("23 thousand per million for males and 18 for females");
  } else if (data == 1920) {
    alert("19 thousand per million for males and 15 for females");
  } else if (data == 1930) {
    alert("17 thousand per million for males and 14.5 for females");
  } else if (data == 1940) {
    alert("20 thousand per million for males and 15 for females");
  } else if (data == 1950) {
    alert("15 thousand per million for males and 11 for females");
  } else if (data == 1960) {
    alert("14.5 thousand per million for males and 9.5 for females");
  } else if (data == 1970) {
    alert("14 thousand per million for males and 8 for females");
  } else if (data == 1980) {
    alert("13 thousand per million for males and 5 for females");
  } else if (data == 1990) {
    alert("10 thousand per million for males and 6 for females");
  } else if (data == 2000) {
    alert("7 thousand per million for males and 5.2 for females");
  } else if (data == 2010) {
    alert("7 thousand per million for males and 4.9 for females");
  }else{
    alert("please enter a valid year from the chart.");
  }
}
</script>

<button onclick="yearData();">Get Year Data</button>
于 2013-10-09T21:55:03.450 回答