2011-04-06 92 views
3

我已經挖掘了所有的教程,並且一直在試圖讓我的頭髮開始工作。Javascript:檢測複選框是否被選中不起作用

onClick觸發正常,但它永遠不會在IF循環內檢查複選框是否已被選中。

<!DOCTYPE html> 
<html> 
<head> 
</head> 

<header> 
<script> 

function updateTotal() { 
document.orderform.total.value = 1*document.orderform.subtotal.value + 1*document.orderform.tax.value + 1*document.orderform.shipping.value; 
} 

**function applyDiscount(x) { 


if (document.orderform.getElementById(x).checked == true) { 
alert("Made it in if"); 
document.orderform.subtotal.value = .7*document.orderform.subtotal.value; 
updateTotal(); 
}** 

} 

</script> 
</header> 

<body> 


<section> 
<h1>H1 here</h1> 

<article> 
<p>Article text here</p> 
</article> 

<form name="orderform" id="orderform" method="post" action="result.php"> 

<label for="firstname">First Name</label> 
<input type="text" name="firstname" id="firstname" placeholder="Enter First Name Here" /> 
<br /> 


<label for="subtotal">Subtotal</label> 
<input type="text" name="subtotal" id="subtotal" value="10" onChange="updateTotal()"/> 
<br /> 

<label for="shipping">Shipping</label> 
<input type="text" name="shipping" id="shipping" value="2" onChange="updateTotal()"/> 
<br /> 

<label for="tax">Tax</label> 
<input type="text" name="tax" id="tax" value="1" onChange="updateTotal()" /> 
<br /> 

<label for="total">Total</label> 
<input type="text" name="total" id="total" value="13" /> 
<br /> 

<label for="discountbox">Apply 30% discount</label> 
<input type="checkbox" name="discountbox" id="discountbox" onClick="applyDiscount(this.id)"/> 

<input type="submit" name="submit" /> 

</form> 

</section> 

<footer> 
<p>Footer here</p> 
</footer> 


</body> 

回答

2

的問題是,你document.orderform是您的形式和getElementById在文件上。

你可以這樣做:

document.getElementById(x).checked

或:

document.orderform.elements[x].checked

+0

謝謝 - 解釋它! – NQQ 2011-04-06 05:37:29

3

你可以嘗試把這個條件

if (document.getElementById('discountbox').checked == true) 
2

試試簡單一點:

if (document.getElementById(x).checked) 
0

幾件事情是錯誤的:

  1. 的,應在標籤。
  2. document.orderform.getElementById需要將線路上的document.getElementById

這在鉻工作對我來說:

<!DOCTYPE html> 
<html> 
<head> 

<script> 

function updateTotal() { 
document.orderform.total.value = 1 * document.orderform.subtotal.value + 1 * document.orderform.tax.value + 1 * document.orderform.shipping.value; 
} 

function applyDiscount(x) { 


if (document.getElementById(x).checked == true) { 
alert("Made it in if"); 
document.orderform.subtotal.value = .7 * document.orderform.subtotal.value; 
updateTotal(); 
} 

} 

</script> 

</head> 
<body> 
<header> 
Header Goes here. 
</header> 

<section> 
<h1>H1 here</h1> 

<article> 
<p>Article text here</p> 
</article> 

<form name="orderform" id="orderform" method="post" action="result.php"> 

<label for="firstname">First Name</label> 
<input type="text" name="firstname" id="firstname" placeholder="Enter First Name Here" /> 
<br /> 


<label for="subtotal">Subtotal</label> 
<input type="text" name="subtotal" id="subtotal" value="10" onChange="updateTotal()"/> 
<br /> 

<label for="shipping">Shipping</label> 
<input type="text" name="shipping" id="shipping" value="2" onChange="updateTotal()"/> 
<br /> 

<label for="tax">Tax</label> 
<input type="text" name="tax" id="tax" value="1" onChange="updateTotal()" /> 
<br /> 

<label for="total">Total</label> 
<input type="text" name="total" id="total" value="13" /> 
<br /> 

<label for="discountbox">Apply 30% discount</label> 
<input type="checkbox" name="discountbox" id="discountbox" onClick="applyDiscount(this.id)"/> 

<input type="submit" name="submit" /> 

</form> 

</section> 

<footer> 
<p>Footer here</p> 
</footer> 


</body> 
</html> 
0

我相信這是你的意思:

<script> 

function updateTotal() { 
    document.getElementById("total").value = 1*document.getElementById("subtotal").value + 1*document.getElementById("tax").value + 1*document.getElementById("shipping").value; 
} 

function applyDiscount(x) { 


if (document.getElementById(x).checked == true) { 
    document.getElementById("subtotal").value = .7 * document.getElementById("subtotal").value; 
    updateTotal(); 
    } 

}