我想按鈕來運行相應的功能字段中的代碼,但我不相信我已經正確設置。應該處理字段中輸入的字符串或數字,然後打印出答案。最後的'else'語句只是查看它是否運行了代碼,但是我可以輸入數字和字符串,並且第一條語句將打印出「This is not a year」。我也不確定是否格式化了第二個'else/if'語句參數(正確輸入%4輸入%100 & &!enter%400)。任何幫助將不勝感激,因爲我試圖自己解決這個問題,並且進展緩慢,所以任何幫助都非常感謝。謝謝!按鈕沒有鏈接到輸入字段與JavaScript
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css" />
<script src='script.js'></script>
<title></title>
</head>
<body>
<!--leap year calculator-->
<h3>Is it a leap year?</h3>
<form action="">
Year:<input type="text" name="enter" id="enter"> <br>
</form>
<script>function YOLO(enter){
if (iNaN(enter)){
console.log("This is not a year");
}
else if (enter % 4 && enter % 100 && !enter % 400){console.log("no");
}
else if(enter % 4){console.log("yes");
}
else {alert("working")}
};
var Id= document.getElementById("enter");
</script>
<button onclick="YOLO(Id)">Enter</button>
</body>
</html>
嘗試enter.value – technosaurus 2013-05-01 12:08:59
您的代碼中有語法錯誤,我想您想使用函數'isNaN'。你用'iNaN'代替。 – 2013-05-01 12:33:32