試圖執行:如果輸入「year」字段的值不是數字(NAN)javascript函數 - 不起作用。我的語法中是否有錯誤?輸入值的比較
謝謝。
<?php
echo "<h1>Testing your Trivia</h1>";
$ages['Casablanca'] = "1943";
$ages['Around The World in 80 Days'] = "1956";
$ages['Patton'] = "1970";
$ages['Annie Hall'] = "1977";
$ages['Chariots of Fire'] = "1981";
$ages['Dances With Wolves'] = "1990";
$ages['Crash'] = "2005";
$ages['The Departed'] = "2006";
$rand_keys = array_rand($ages, 1);
?>
<script language="JavaScript" type="text/javascript">
function validate (form)
{
var valid = "1234567890";
if (form.year.value == "" || isNaN(valid)) {
alert("Please enter a year.");
return false;
}
return true;
}
</script>
<form method='post' name="inputyear" onsubmit="return validate(this);">
Give the year below won academy award<br>
<Strong>Movie:</strong> <input type='text' name='movie' value='<?= $rand_keys ?>' readonly='readonly' /><br>
<Strong>Year it Won the Oscar:</Strong> <input type='text' name='year' size="30" /><br/>
<input type='submit' name='submit' />
</form>
<?php
echo '<pre>';
foreach($ages as $movie => $year){
print_r("Year: $year, Title: $movie <br />");
}
echo '</pre>';
if($_POST['submit']) {
$movie = $_POST['movie'];
$year = $_POST['year'];
$realyear = $ages[$movie];
echo "<h2>Answer for: $movie</h2>";
if(array_key_exists($movie, $ages)) {
echo "Your answer: $year<br/>";
}
echo "Correct Answer: $realyear<br/>";
if ($realyear == $year) {
echo "<strong style='color:green;'>WELL DONE</strong><br/>";
} else {
// wrong
echo "<strong style='color:red;'>Incorrect.</strong><br/>";
}
}
?>
你想向用戶展示一些電影名稱,然後讓他猜猜電影被授予了哪一年,我理解它是對的嗎? – Chvanikoff 2011-02-03 17:06:56