【问题标题】:How to iterate or loop using each() in jQuery / javascript如何在 jQuery / javascript 中使用 each() 进行迭代或循环
【发布时间】:2012-01-21 16:39:10
【问题描述】:

有人可以帮我在表格的所有文本输入中迭代或循环我的计算输出吗?输出显示但仅显示在一行中,而不显示在表格的另一行中。非常感谢帮助我解决这个问题。

<head>
<script language="javascript" type="text/javascript">

function calculateSellingPrice()
{
   var txtcost = document.getElementById("cost");
   var txtsellingprice = document.getElementById("sellingprice");
   var txtprofit = document.getElementById("profit");


  //find the total first
  var total = parseFloat(txtcost.value) + parseFloat(txtpostage.value);

  //for selling price computation
  txtsellingprice.value = total;

 //for ebay fees computation
 if (txtsellingprice.value > 50){
   txtebayfees.value = parseFloat(txtsellingprice.value - 50) * 0.05 + 3.5;
 }
 else if (txtsellingprice.value <= 50){
 txtebayfees.value = parseFloat(txtsellingprice.value) * 0.07;
 }
 }
//-->
</script>

</head>
<body onload="calculateSellingPrice();">    
<table> 
  <tr>
    <td><center>SKU</center></td>
    <td><center>Selling Price</center></td>
    <td><center>Cost</center></td>
  </tr>    
      <?php
      $result = mysql_query("SELECT sku, sellingprice FROM tbl_inventory");                               
      while ($myrow = mysql_fetch_row($result))
      { 
    ?>
      <tr>
    <td>
       <?php echo "<input type='text' id='sku' name='sku' />"; ?>
    </td>
    <td>
       <?php echo "<input type='text' id='sellingprice' name='sellingprice' />"; ?>
    </td>
    <td>
       <?php echo "<input type='text' id='cost' name='cost' />"; ?>
    </td>
     </tr>  
    <?php>
    }
    ?>
</table>

【问题讨论】:

  • 您是在问如何修复您的 PHP 模板文件,使其输出到多行吗?或者您是在问如何使用 javascript 在客户端做一些事情?你的问题写得不清楚。另外,如果您不是在问 PHP 问题,那么请发布浏览器看到的生成的 javascript(您可以在浏览器中使用 View/Source 来捕获它),以便我们可以看到浏览器实际看到的内容。
  • 我正在寻找一些使用 javascript 的客户端计算,并在输入文本上迭代计算的数据。你能给我一些相关的想法吗?

标签: php jquery jquery-selectors


【解决方案1】:

只是指出一些问题,您正在查询您的表,您需要根据返回的值进行计算,对吧?

也许你需要一个值来计算一些东西..看看下面我是怎么做的..

<?php echo "<input type='text' id='sellingprice' name='sellingprice' value='".$myrow['sellingprice']."' />"; ?>

你错过了这部分 -> value='".$myrow['sellingprice']."'

【讨论】:

    【解决方案2】:

    首先:请不要写bolt。

    第二:如果需要表格,请使用 jQuery Plugin Datatables.net。太棒了!

    只需在 PHP 中创建一个如下所示的数值数组:

    $x = array(0 => array(
                0 => 'this is a name',
                1 => 'this is a price',
                2 => 'this is a x')
          0 => array(
                0 => 'this is a name',
                1 => 'this is a price',
                2 => 'this is a x')
    );
    

    那就json_encode(array('aaDara =&gt; $x));

    并将其用作 AjaxSource。真的很不错!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-07
      • 2020-10-21
      • 2010-09-11
      • 2012-09-24
      • 1970-01-01
      • 2019-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多