這是我的html代碼:如何檢查單選按鈕使用JavaScript
<input type="radio" name="sex" id="boy" value="male" /> Boy<p></p><br>
<input type="radio" name="sex" id="girl" value="female" />Girl
<button id="fine" type="button">Submit</button>
,這是在我的JavaScript文件:
var maskio = document.getElementById('boy');
var femmna = document.getElementById('girl');
function prima(){
var fine = document.getElementById('fine');
fine.onclick=chek;
}
function chek(){
if((maskio.checked == false) && (femmna.checked == false)) {
alert('lol');
return false;
}
}
window.onload=prima;
問題出在哪裏?當我運行它並單擊提交沒有任何反應。爲什麼?
_「......沒有任何反應。」 _ - 什麼都沒有?所以你沒有得到類似'Uncaught TypeError的錯誤:無法在瀏覽器的控制檯中讀取'null'屬性'檢查'?我認爲你的JS文件包含在文檔的'
'部分? – nnnnnn你可以簡單地解決這個問題是這樣的:http://jsfiddle.net/TQ3Xt/1/ – Stano