duandanxiu6965 2016-01-20 07:12
浏览 24

如何将php中的while循环内的2个文本框的数据与javascript相乘?

Once I use the script I did in JavaScript, it only changes the value of the first textbox inside a while loop. Here is my php code:

<?php 
  $edittable=$_POST['selector'];
  $N = count($edittable);
  for($i=0; $i < $N; $i++)
  {
    $result = mysql_query("SELECT * FROM inventory where assetid='$edittable[$i]'");
    while($row = mysql_fetch_array($result))
    {
      extract($row);
?>      <tr>
          <td></td>
          <td><?php echo $assetdesc; ?></td>
          <td><?php echo $serialno ?></td>
          <td><select class="form-control" name="qtyunit" id="qtyunit" onchange="calculate()"  required>
                  <option>----</option>
                  <?php
                  $qtyfirst = 1;
                for($new = $qtyfirst ; $new <= $assetqty; $new++){
                   ?>
                  <option><?php echo $new; ?></option>
                  <?php
                }
                     ?>
                     </select></td>
          <td><?php echo "₱".number_format((float)$purchaseprice, 2, '.', ','); ?></td>
          <input value = "<?php echo $purchaseprice; ?>" name="hiddencost" id="hiddencost" hidden>
          <td><input class="form-control" name="totalcost" id="totalcost" readonly></td>
        </tr>
<?php 
    }
  }
?>

Here is my JavaScript:

<script type="text/javascript">
function calculate() {
var myBox1 = document.getElementById('qtyunit').value; 
var myBox2 = document.getElementById('hiddencost').value;
var result = document.getElementById('totalcost'); 
var myResult = myBox1 * myBox2;
result.value = myResult;
 }
 </script>
  • 写回答

2条回答 默认 最新

  • dongxin8709 2016-01-20 07:57
    关注

    the biggest problem I can see here without testing is that you are using the same HTML id attribute multiple times when looping your output with PHP.
    you can only use one id name once per document. if you want to work with multiple html elements at once you have to use classes, for example your select:

    <select class="form-control" name="qtyunit" class="qtyunit" onchange="calculate()"  required>
        <option>----</option>
        <?php
            $qtyfirst = 1;
            for($new = $qtyfirst ; $new <= $assetqty; $new++){
        ?>
        <option>
            <?php echo $new; ?>
        </option>
        <?php
            }
        ?>
    </select>
    

    and then in your javascript:

    var myBoxes = document.getElementsByClassName('qtyunit').value; 
    

    note: this example might not be what you want to achieve - I just wanted to point out your primary mistake.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测