Javascript:检测是否选中复选框不工作
Javascript: detecting if checkbox checked not working
我已经翻遍了所有的教程,一直在绞尽脑汁想让这个工作。
onClick正确触发,但它永远不会进入IF循环,在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>
你能试着把这个条件
if (document.getElementById('discountbox').checked == true)
问题是你的document.orderform
是你的表单,getElementById
是文档。
你可以做:
document.getElementById(x).checked
或:
document.orderform.elements[x].checked
试试简单一点的:
if (document.getElementById(x).checked)
有几个地方不对:
- 行document.orderform.getElementById需要是document.getElementById
这对我在chrome:
<!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>
我相信这就是你的意思:
<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();
}
}
相关文章:
- 复选框一次选中一个不在gridview中工作
- 更改无法正常工作的复选框的属性
- 复选框选择/取消选择触发器第一次工作,但在随后的尝试中失败
- 复选框在手动触发后停止工作
- 复选框的checked属性的修改不会;工作不正常
- 我正试图获得一个带有复选框的多选下拉列表,在填充项目和获得所有选中的工作时遇到了一些问题
- ajax后表单复选框更改时jquery ajax不工作
- 当选中一个复选框时,表单中的所有其他复选框都必须禁用-不工作
- 检测Bootstrap Switch Jquery中的类更改(将工作复选框JS转换为按钮JS)
- jQuery检查所有复选框只工作一次
- Rally自定义应用程序复选框筛选器不工作
- Angular/Ionic/Javascript:无法让下拉过滤器作为复选框工作
- 设置 JQuery HTML javascript 复选框工作,但不显示为选中状态
- 似乎无法获得示例代码来替换复选框工作
- 如何获得螺丝默认按钮插件与动态添加的复选框工作
- 如何使复选框工作作为单选按钮列表
- 选中所有带有样式javascript类的复选框'工作
- 禁用复选框工作,但它没有在onchange上被处理
- 为什么第一个复选框工作,而第二个不工作
- 如何使此复选框工作