简体   繁体   English

如何检查单选或复选框?

[英]How can I check if radio or checkbox is checked?

I have this HTML code: 我有这个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>

and this is part of vypisForm function: 这是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é");
   }

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 谢谢

You're trying to test for checked on the value. 您正在尝试测试是否已检查该值。

Instead of: 代替:

var cc1 = document.getElementById("cc1").value;

Use: 采用:

var cc1 = document.getElementById("cc1").checked;
if (cc1) {
    document.write(cc1 + "<br>");
}

Because both cc1 and cc2 have to be the checkboxes themsevles not their values: 因为cc1cc2都必须是复选框,所以它们的值不是其值:

var cc1 = document.getElementById("cc1");
//                                     ^^^ remove .value

您不需要document.getElementById.value仅是该对象,因此取消该.value

document.getElementById("t1").value gets the value of the input, but it doesn't hold its checked status. document.getElementById(“ t1”)。value获取输入的值,但不保留其检查状态。 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é");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM