我有這樣的HTML代碼:如何檢查收音機或複選框是否被選中?
<div id="f4" class="none">
<h4>Jaký tarif chcete zvolit?</h4>
<input type="radio" name="tarif" value="tarif1" id="t1"><label for="t1">Tarif 1</label> <br>
<input type="radio" name="tarif" value="tarif2" id="t2"><label for="t2">Tarif 2</label> <br>
<input type="radio" name="tarif" value="tarif3" id="t3"><label for="t3">Tarif 3</label> <br>
<h4>Co chcete aktivovat?</h4>
<input type="checkbox" name="akt" value="roaming" id="cc1"><label for="cc1">Roaming</label> <br>
<input type="checkbox" name="akt" value="platby" id="cc2"><label for="cc2">Platby</label> <p></p>
<input type="button" name="send" value="ODESLAT" onclick="vypisForm();">
</div>
,這是vypisForm功能的一部分:
var t1 = document.getElementById("t1").value;
var t2 = document.getElementById("t2").value;
var t3 = document.getElementById("t3").value;
var cc1 = document.getElementById("cc1").value;
var cc2 = document.getElementById("cc2").value;
if(t1.checked) {
document.write("Zvolený tarif je: "+ t1 + "<br>");
} else if(t2.checked) {
document.write("Zvolený tarif je: "+ t2 + "<br>");
} else {
document.write("Zvolený tarif je: "+ t3 + "<p></p>");
}
document.write("Zvolili jste tyto služby:" + "<br>");
if (cc1.checked) {
document.write(cc1 + "<br>");
} else if (cc2.checked) {
document.write(cc2 + "<br>");
} else if(cc1.checked && cc2.checked) {
document.write(cc1 + "<br>");
document.write(cc2 + "<br>");
} else if(!cc1.checked && !cc2.checked) {
document.write("Žádné");
}
但它不工作..想法是,如果單選按鈕被選中,寫和如果單選按鈕被選中寫另一回事..
與相同的複選框,有兩個那些..
有誰看到I D o不?謝謝
那麼你引用的值,你正在查看該字符串的選中值.....而你不應該使用document.write。 – epascarello
好的謝謝..這只是爲了檢查,我得到正確的數據.. –