Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript计算不适用于第二个文本字段_Javascript_Html - Fatal编程技术网

Javascript计算不适用于第二个文本字段

Javascript计算不适用于第二个文本字段,javascript,html,Javascript,Html,我在JSBin上发现了一些代码,它的工作方式就像一种款待,直到我尝试调整它来做更多的事情!现在我似乎有一个错误,它不会计算第二个字段 我编辑的代码可以在下面看到 <!DOCTYPE html> <!-- Created using JS Bin http://jsbin.com Copyright (c) 2015 by anonymous (http://jsbin.com/fivesocaku/1/edit) Released under the MIT license

我在JSBin上发现了一些代码,它的工作方式就像一种款待,直到我尝试调整它来做更多的事情!现在我似乎有一个错误,它不会计算第二个字段

我编辑的代码可以在下面看到

<!DOCTYPE html>
<!--
Created using JS Bin
http://jsbin.com

Copyright (c) 2015 by anonymous (http://jsbin.com/fivesocaku/1/edit)

Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>

<table width="80%" border="0">
  <tr>
    <th>Qty</th>
    <th>1.2m</th>
    <th></th>
    <th>1.8</th>
    <th></th>
  </tr>
  <tr>
    <td><input id="qty" type="text" oninput="calculate()" /></td>
    <td><input id="R12" type="text" oninput="calculate()" /></td>
    <td><input id="b12" type="hidden" value="5" oninput="calculate()" /></td>
    <td><input id="R18" type="text" oninput="calculate()" /></td>
    <td><input id="B18" type="hidden" value="2" oninput="calculate()" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

<script id="jsbin-javascript">
    function calculate() {
        var myBox1 = document.getElementById('qty').value;  
        var myBox2 = document.getElementById('b12').value;
        var result = document.getElementById('R12');    
        var myResult = myBox1 * myBox2;
        result.value = myResult;
        var myBox4 = document.getElementById('b18').value;
        var result18 = document.getElementById('R18');  
        var myResult18 = myBox1 * myBox4;
        result.value = myResult;

    }
</script>
</body>
</html>
我似乎没有收到任何错误报告,但想知道是否有人可以看一看,让我知道我的错误在哪里

谢谢

var myBox4 = document.getElementById('b18').value;
b18必须是大写


您尝试通过id b18而不是b18获取元素。 此外,您将再次将该值指定给result,而不是result18

如果我能提供我的2美分,对变量使用更有意义的名称,并有一些相同的大小写模式,带/带我们的数字等等

    var myBox4 = document.getElementById('b18').value;
您得到的元素id不存在

   var result18 = document.getElementById('R18');  
   var myResult18 = myBox1 * myBox4;
   result.value = myResult;

最后一个作业应该是result18.value=myResult,您将两次分配到同一文本框。

谢谢大家的帮助。
   var result18 = document.getElementById('R18');  
   var myResult18 = myBox1 * myBox4;
   result.value = myResult;