我非常了解 Javascript 和 HTML,剛開始學習 IT,這是我的第一次評估。本質上,任務是 - 提供一個下拉串列和一個年齡輸入框,提供一個“提交”按鈕并添加一個帶有 JS 的警告框,顯示用戶是否輸入了正確的詳細資訊以繼續。(新西蘭和 18(或更高)是“正確”答案繼續 - 其他國家選項和低于 18 得到拒絕警報答案)
到目前為止,這是我的 HTML:
<body>
<form method="get">
<select id="country" name="country">
<option value="0">PLEASE SELECT YOUR COUNTRY</option>
<option value="1">New Zealand</option>
<option value="2">Australia</option>
<option value="3">Switzerland</option>
</select>
<input type="number" id="dob">
</form>
<button type="button" id="submit" >SUBMIT</button>
<script src="test.js"></script>
</body>
我嘗試過使用 Javascript(如下所示的 var)和函式(我對此感到困惑并已洗掉):
var select = document.getElementById("country");
document.getElementById("button").onclick = function(){
var value = select.value
if (value == "1")
{
alert("Thanks");
}
if (value == "2")
{
alert("sorry");
}
if (value == "3")
{
alert("sorry");
}
if (value == "0")
{
alert("sorry");
}
};
uj5u.com熱心網友回復:
您在 document.getElementById("button").onclick 中的 id 是錯誤的。id 是提交不是按鈕
var select = document.getElementById("country");
document.getElementById("submit").onclick = function(){
var value = select.value
if (value == "1")
{
alert("Thanks");
}
if (value == "2")
{
alert("sorry");
}
if (value == "3")
{
alert("sorry");
}
if (value == "0")
{
alert("sorry");
}
};
我已經重構了你的代碼。它應該像這樣運行
const select = document.getElementById("country");
const inp = document.getElementById("dob");
document.getElementById("submit").onclick = function(){
const selected = select.value;
const dob = parseInt(inp.value);
if( selected === "1" && dob >= 18 ){
alert("Thanks");
} else {
alert("Sorry");
}
};
<form method="get">
<select id="country" name="country">
<option value="0">PLEASE SELECT YOUR COUNTRY</option>
<option value="1">New Zealand</option>
<option value="2">Australia</option>
<option value="3">Switzerland</option>
</select>
<input type="number" id="dob">
</form>
<button type="button" id="submit" >SUBMIT</button>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/476265.html
標籤:javascript html 落下 提交 警报
上一篇:表過濾器/搜索兩列
