为什么for循环不能使用keyup函数在javascript中工作?

时间:2016-10-04 05:36:04

标签: javascript html keyup

当我在文本框中输入金额时,它进入功能状态而不是循环工作。

我有一个表显示多个动态行。

行数仅计算函数中的count.value(javascript)

然后因为这个多行我只想要循环来增加价格(rec_qty * porate)

HTML

<?php
$table=mysql_query("SELECT * from purchase_order");
while($table1=mysql_fetch_array($table))
{   
  $i++;

<tr>
<td><input type="hidden" id="tatalrecord" value="<?php echo $i;?>"></td>
<td><input type="text" style="width:45px" class="form-control rec_qty" onkeyup="myFunction()" id="rec_qty"></td>

<td><input type="text" style="width:45px" class="form-control porate" onkeyup="myFunction()" id="porate"></td>
</tr>
<?php } ?>
</table>

JAVASCRIPT

function myFunction() {
  var count = document.getElementById("tatalrecord");
  var cn = count.value;
  alert(count.value);
    for(var i = 0; i < cn; ++i)
    {
      alert("hi");
      var x = document.getElementById("rec_qty");
      var y = document.getElementById("porate");
      var z = x.value * y.value;
    } 
 }

3 个答案:

答案 0 :(得分:1)

&#13;
&#13;
function myFunction() {
  var z = 0;
  var count = document.getElementById("tatalrecord");
            var cn = (count.value) ? parseInt(count.value) : 0;
            alert(count.value);
            for (var i = 0; i < cn; ++i)
            {
                var x = document.getElementById("rec_qty");
                var y = document.getElementById("porate");
                 z = parseInt(x.value) * parseInt(y.value);
            }
  
  
  document.getElementById("total").value = z
        }
&#13;
<html>
<head>
<script> 
  
</script>
</head>
<body>
<table>
<tr>
    <input type="text" style="width:45px" value='2' id="tatalrecord">
<td><input type="text" style="width:45px" class="form-control rec_qty" onkeyup="myFunction()" id="rec_qty"></td>

<td><input type="text" style="width:45px" class="form-control porate" onkeyup="myFunction()" id="porate"></td>
</tr>
</table>
  
  total : <input type="text" style="width:45px" class="form-control porate" value="0" id="total">
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

java.lang.VerifyError: JVMVRFY013 class loading constraint violated; class=org/apache/axis/MessageContext, method=getMessage()Ljavax/xml/soap/SOAPMessage;, pc=0.
JVMVRFY013 class loading constraint violated; class=org/apache/axis/MessageContext, method=getMessage()Ljavax/xml/soap/SOAPMessage;, pc=0

的Javascript

<table>
<tr>
<td> Price </td>
<td> Quantity </td>
<td> Total  </td>
<tr>
<?php  
  $table = mysql_query("select * from table_name");
  $i=0;
  while($row = mysql_fetch_array($table))
  {
?>
  <tr>
  <td> <input type="hidden" id="tatalrecord" value="<?php echo $i; ?>"></td>
  <td> <input type="text" id="porate<?php echo $i; ?>" onkeyup="myFunction()" value="<?php echo $i; ?>"></td>
  <td> <input type="text" id="rec_qty<?php echo $i; ?>" onkeyup="myFunction()" value="<?php echo $i; ?>"></td>
  <td> <input type="text" readonly="" id="total"> </td>
  </tr>
}
</table>

答案 2 :(得分:0)

嗨Shruti我试图缩短你的问题我已经创建了自己的表来假设表格行

希望这段代码可以帮助你......

<html>
<head>
    <script type="text/javascript">
        function myFunction() {

          // var count = document.getElementById("tatalrecord");

          //Total no of rows in your table
          var cn =  document.getElementById("tatalrecord").getElementsByTagName("tr").length;

          console.log(cn);

            for(var i = 0; i < cn; ++i)
            {
              console.log("hi");
              alert('hi');
              var x = document.getElementById("rec_qty");
              var y = document.getElementById("porate");
              var z = x.value * y.value;
              console.log(z);
            } 
         }
    </script>
</head>
<body>
    <form>
        <table id="tatalrecord">
        <tr><td>1</td></tr>
        <tr><td>2</td></tr>
        <tr><td>3</td></tr>
        <tr><td>4</td></tr>
        <tr><td>5</td></tr>
        </table>

        <input type="text" style="width:45px" class="form-control rec_qty" onkeyup="myFunction()" id="rec_qty">
        <input type="text" style="width:45px" class="form-control porate" onkeyup="myFunction()" id="porate">

    </form>
</body>
</html>