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ですが、たとえばボタンをクリックするとこのプロンプトがアクティブになるようにしたいです

日付を取得するには [ここをクリック] -----> 「何年のデータが必要ですか?」

どうすればいいですか。また、このボタンはhtmlに入る必要があります。

4

2 に答える 2

1

これを試して。ロジックは関数内にあり、ボタンでクリック イベントがトリガーされたときに実行されます。

<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 に答える