当我勾选两个复选框时,为什么不显示标签?

时间:2015-12-02 16:37:18

标签: javascript html

JavaScript的:

 function Checkbox() {
//Deleted parenthesis below, because checked is a property, not a function
var FirstClass = document.getElementById("First-Class");
var Standard = document.getElementById("Standard");

if (FirstClass.checked && Standard.checked) || !FirstClass.checked && !Standard.checked) {
   // document.getElementById("CheckError").textContent = "";
    alert("Please");
}
else {
    document.getElementById("CheckError").textContent = "Please Ensure everything is covered.";
}

}
HTML:

<header class="main-header">  <!--- Header container for the logo and title test--->
<div class="main-logo"><h2><a href="#"><img src="img/main-logo.png"></a></h2></div>  <!---Railine Logo-->
<h1>ailLine</h1>       
</header>

  <input type="checkbox" id="Standard" name="Type" value="Standard">
<label class="light" for="Standard">Standard</label><br>

<input type="checkbox" id="First-Class" name="Type" value="First-Class">
<label class="light" for="First-Class">First Class</label><br><br>

<label id = "CheckError"></label>

<p id="total-cost"></p>
<button type = "button" value="checkout" id="checkoutbtn" onclick="Checkbox()" >CHECKOUT</button>




<!------------END OF SECOND FORM--------->           

你好伙计们在这里我试图制作一个标签显示如果两个复选框都是空的,我怎么能这样做我尝试但没有任何反应。

2 个答案:

答案 0 :(得分:2)

这对我有用:

HTML:

postcodes

使用Javascript:

<input type="checkbox" id="Standard" name="Type" value="Standard">
<label class="light" for="Standard">Standard</label><br>

<input type="checkbox" id="First-Class" name="Type" value="First-Class">
<label class="light" for="First-Class">First Class</label><br><br>

<label id = "CheckError"></label>

<p id="total-cost"></p>
<!-- Assign click event on JS script -->
<button type = "button" value="checkout" id="checkoutbtn" onclick="myFunction(); createcookie(); AdultNumber(); calculateFare();" >CHECKOUT</button>

现在,&#34; FirstClass&#34;和&#34;标准&#34;变量是布尔值,因此您不必在if条件中使用.checked属性。

答案 1 :(得分:1)

您的代码有效,此行只有一点语法错误:

if ((FirstClass.checked && Standard.checked) || (!FirstClass.checked && !Standard.checked)) {

你错过了一些括号......

Codepen:http://codepen.io/anon/pen/JGPpWG