【问题标题】:Javascript: detecting if checkbox checked not workingJavascript:检测复选框是否检查不起作用
【发布时间】:2011-07-30 12:20:15
【问题描述】:

我已经阅读了所有的教程,并且一直在努力尝试让它发挥作用。

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>

【问题讨论】:

    标签: javascript checkbox onclick


    【解决方案1】:

    你能试着把这个条件放在一边

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

    【讨论】:

      【解决方案2】:

      问题是您的 document.orderform 是您的表单,而 getElementById 在文档中。

      你可以这样做:

      document.getElementById(x).checked

      或:

      document.orderform.elements[x].checked

      【讨论】:

      • 谢谢你 - 这解释了它!
      【解决方案3】:

      尝试一些更简单的东西:

      if (document.getElementById(x).checked)
      

      【讨论】:

        【解决方案4】:

        有几处是错误的:

        1. 应该在标签中。
        2. 行 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>
        

        【讨论】:

          【解决方案5】:

          我相信这就是你的意思:

          <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();
              }
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-08-13
            • 2016-03-13
            • 2023-03-03
            • 1970-01-01
            • 2011-01-13
            • 1970-01-01
            相关资源
            最近更新 更多