简体   繁体   English

为什么我的if语句不能以document.getElementById(“ ID”)。value = var结尾?

[英]Why can't I end my if statement with document.getElementById(“ID”).value = var?

I am writing a price checkbox page for my web programming class using javascript where if you check on the boxes and click subtotal, the price is put into a textbox. 我正在使用javascript为我的Web编程类编写一个价格复选框页面,如果您选中这些复选框并单击小计,则价格会放入文本框。 The issue I am having is when I end my if statements with document.getElementById("subtotal").value = total it isn't populating the textbox. 我遇到的问题是当我用document.getElementById("subtotal").value = total结束if语句时,它没有填充文本框。 If I insert an alert or document.write statement then it will populate it. 如果我插入一个alertdocument.write语句,那么它将填充它。 Any idea what I am missing? 知道我缺少什么吗? here is my code with the alerts in place (that I don't want): 这是我的代码,其中有警报(我不想要):

<!DOCTYPE html>

<head>
    <title> Price Checkbox </title>
    <meta charset = "utf-8" />
    <script>
        var total, a = 0;
    </script>
    <script type = "text/javascript">
        function subtotal1()
        {
            if (document.getElementById('apple').checked)
            {
                total = a + .59;
                a = total;
                document.getElementById("subtotal").value = total
                alert(total);
            }
            if(document.getElementById('orange').checked)
            {
                total = a + .49;
                a = total;
                document.getElementById("subtotal").value = total
                alert(total);
            }
            if(document.getElementById('banana').checked)
            {
                total = a + .39;
                a = total;
                document.getElementById("subtotal").value = total
                alert(total);
            }
        }
    </script>
 </head>
 <body>

     <form id = "myForm"  action = "">
         <p>
            <label> <input type = "checkbox"  id = "apple"/>
            apple $.59 </label>
            <br />
            <label> <input type = "checkbox"  id = "orange"/>
            orange $.49 </label>
            <br />
            <label> <input type = "checkbox"  id = "banana"/>
            banana $.39 </label>
         </p>

         <button onclick = "subtotal1()">Submit</button>
         <p></p>
         <input type="textbox" id="subtotal"/>
     </form>

</body>

I suggest a different approach. 我建议使用另一种方法。 Add an event when the checkbox is clicked. 单击复选框时添加事件。 This way you can also see what is happening and not leave it when submitting the form. 通过这种方式,您还可以查看正在发生的事情,而在提交表单时却不会离开。 Try this code: 试试这个代码:

<body>

     <form id = "myForm"  action = "">
         <p>
            <label> <input type = "checkbox"  id = "apple"  onclick = "subtotal1();"/>
            apple $.59 </label>
            <br />
            <label> <input type = "checkbox"  id = "orange"  onclick = "subtotal1();"/>
            orange $.49 </label>
            <br />
            <label> <input type = "checkbox"  id = "banana"  onclick = "subtotal1();"/>
            banana $.39 </label>
         </p>

         <button>Submit</button>
         <p></p>
         <input type="textbox" id="subtotal"/>
     </form>

</body>

暂无
暂无

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

相关问题 为什么我不能用 `var output = document.getElementById('message').innerHTML;` 做一个循环 - why can't I do a loop with `var output = document.getElementById('message').innerHTML;` 为什么不能通过document.getElementById(“ node&#39;s_id”)。child选择子节点? - Why can't I select nodes child by document.getElementById(“node's_id”).child? JavaScript &gt; 如果我设置“var x = document.getElementById('inputText').value”并更新“x”,为什么值没有更新? - JavaScript > If I set “var x = document.getElementById('inputText').value” and I update “x”, why doesn't the value update? 如何重置我的 document.getElementById 颜色值 - How can i reset my document.getElementById color value 我无法从 `document.getElementById` 中获取值 - I can’t get the value from `document.getElementById` 我无法获得的价值<texterea />使用 document.getElementById() - I can't get the value of <texterea /> using document.getElementById() var x=document.getElementById(“mytext”).value;为什么我将值更改为innerHTMl 或innerText,它不起作用? - var x=document.getElementById(“mytext”).value;Why i change the value to innerHTMl or innerText,it do not work? “ var canvas = document.getElementById(“ canvas”)”不能为全局变量吗? - “var canvas = document.getElementById(”canvas“)” can't be a global variable? 无法访问属性 "innerHTML", document.getElementById('id' +num) 即使 js 在末尾声明 - can't access property "innerHTML", document.getElementById('id' +num) even though js is declared at the end of <body> 为什么querySelector('#id')映射到document.getElementById('id')? - Why doesn't querySelector('#id') map to document.getElementById('id')?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM