2013-10-09 54 views
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這顯然開放的提示和要求的日期,但我想這提示上點擊按鈕被激活,例如我想Javascript代碼鏈接到一個按鈕

要獲取日期[點擊此處] ----->「您希望獲得哪些數據?」

我該怎麼做。此按鈕也需要進入html。

+2

在函數內部獲取該代碼。有按鈕的點擊事件調用該函數。 – Renan

+2

你可能想要做一些研究並試一試。查看此頁面上的「相關」側邊欄。告訴我們你的嘗試和錯誤。 「詢問代碼的問題必須對所解決的問題有最小的理解,包括**嘗試的解決方案**,**爲什麼他們不工作**,以及預期的結果。」 – showdev

+0

非常感謝你的幫助。愚蠢的我忘記把它放在一個函數 –

回答

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> 
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()

+0

我建議編輯'var promptForYearAndReport(){'''var promptForYearAndReport = function(){'因爲目前您的代碼不會工作,但它被拒絕.... – rorypicko

+0

+1 @ RoryPicko92:感謝您選擇那一款。我不確定它爲什麼被拒絕。 –

相關問題