如何检查单选或复选框是否已选中? [英] How can I check if radio or checkbox is checked?

查看:72
本文介绍了如何检查单选或复选框是否已选中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML代码:

I have this HTML code:

   <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函数的一部分:

and this is part of vypisForm function:

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é");
   }

但是它不起作用..想法是,如果选中该单选按钮,请编写此代码,如果选中该单选按钮,请编写另一件事.

But it does not work.. Idea is that if that radio button is checked, write this and if that radio button is checked write another thing..

与复选框相同,其中有两个.

Same with the checkboxes, there are two of those..

有人看到我没有看到的东西吗?谢谢

Does anybody see what I do not?? Thanks

推荐答案

document.getElementById("t1").value获取输入的值,但不保持其检查状态.这就是你想要做的.

document.getElementById("t1").value gets the value of the input, but it doesn't hold its checked status. Here is what you want to do.

var t1 = document.getElementById("t1");
var t2 = document.getElementById("t2");
var t3 = document.getElementById("t3");
var cc1 = document.getElementById("cc1");
var cc2 = document.getElementById("cc2");
if(t1.checked) {
   document.write("Zvolený tarif je: "+ t1.value + "<br>");
} else if(t2.checked) {
   document.write("Zvolený tarif je: "+ t2.value + "<br>");
} else {
   document.write("Zvolený tarif je: "+ t3.value + "<p></p>");
}

document.write("Zvolili jste tyto služby:" + "<br>");
if (cc1.checked) {
   document.write(cc1.value + "<br>");
} else if (cc2.checked) {
   document.write(cc2.value + "<br>");
} else if(cc1.checked && cc2.checked) {
   document.write(cc1.value + "<br>");
   document.write(cc2.value + "<br>");
} else if(!cc1.checked && !cc2.checked) {
   document.write("Žádné");
}

这篇关于如何检查单选或复选框是否已选中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆