在書上學習到如何選取到正確的radio選項並跳出alert視窗顯示good或sad
可是我的函式放在原網頁的script裡面可以實行,放到額外另加的js網頁就無法跳出alert
可以幫我看一下是哪裡出了問題嗎? 感謝
元網頁 test4.html
<html>
<head>
<meta charset="utf-8"/>
<title>javascript</title>
<script type= "text/javascript" language= "javascript" src= "javascript2.js" >
</script>
</head>
<body>
<h3>加法<h3>
<form name="form1" onsubmit="kuwaeru();return false;">
20+16=?
<input type="radio" name="answer" value="2016" required>2016
<input type="radio" name="answer" value="36" required>36
<input type="radio" name="answer" value="99" required>99
<input type="radio" name="answer" value="22" required>22
<input type="submit">
</form>
</body>
</html>
額外另家的網頁 javascript2.js
function kuwaeru(){
var all = document.forms["form1"].elements["answer"];
if(all[1].checked)
alert("good");
else alert("sad");
}
有問題再提出來:)
<html>
<head>
<meta charset="utf-8"/>
<title>javascript</title>
<script >
function kuwaeru(){
var all1 = document.forms["form1"].elements["answer1"];
var all2 = document.forms["form1"].elements["answer2"];
var all3 = document.forms["form1"].elements["answer3"];
var all4 = document.forms["form1"].elements["answer4"];
if(all2.checked)
alert("good");
else alert("sad");
}
</script>
</head>
<body>
<h3>加法<h3>
<form name="form1" onsubmit="kuwaeru();">
20+16=?
<input type="radio" name="answer1" value="2016" >2016
<input type="radio" name="answer2" value="36" >36
<input type="radio" name="answer3" value="99" >99
<input type="radio" name="answer4" value="22" >22
<input type="submit">
</form>
</body>
</html>
看這個是否符合您的需求
<html>
<head>
<meta charset="utf-8"/>
<title>javascript</title>
<script >
function kuwaeru(){
var all1 = document.forms["form1"].elements["answer1"];
var all2 = document.forms["form1"].elements["answer2"];
var all3 = document.forms["form1"].elements["answer3"];
var all4 = document.forms["form1"].elements["answer4"];
if(form1.answer1.checked == "" && form1.answer2.checked == "" && form1.answer3.checked == "" && form1.answer4.checked == "")
{
alert("請點選選項提交。");
}
else {
if(all2.checked)
alert("good");
else alert("sad");
}
}
</script>
</head>
<body>
<h3>加法<h3>
<form name="form1" onsubmit="kuwaeru()">
20+16=?
<input type="radio" name="answer1" value="2016" >2016
<input type="radio" name="answer2" value="36" >36
<input type="radio" name="answer3" value="99" >99
<input type="radio" name="answer4" value="22" >22
<input type="submit" >
</form>
</body>
</html>