显示产品描述的成本以在每一行上工作


Display cost of product description to work on every line

我是初学者,我正在尝试从MYSQL数据库中获取成本以处理每一行。 我创建了两列成本来显示我尝试的内容。 到目前为止,我可以得到成本,但我只能让它在最后一行工作class = "<?php echo $line_item; ?>"我也尝试使用"class = results"它显示正确的成本,但它显示在每一行上。 我知道这看起来一团糟,但非常感谢任何帮助

<?php
include '****';
$order_num = $_REQUEST['order_n'];
$result = mysql_query("SELECT * from items where order_n=$order_num"); ?>
<script src="javascripts/jquery.js" type="text/javascript"></script>
<script src="javascripts/jquery.dataTables.js" type="text/javascript"></script>
<table id='datatables' class='display' cellspacing='1'  table width = '1200'  border = '5'' div >
 <thead>
 <tr>                 
      <th><div align='left'><span class='style7'>Line</span></div></th> 
      <th><div align='left'><span class='style7'>Order #</span></div></th>
      <th><div align='left'><span class='style7'>Choose Product</span></div></th>           
      <th><div align='left'><span class='style7'>cost</span></div></th>
      <th><div align='left'><span class='style7'>cost</span></div></th>      
  </tr>
  </thead>
  <?php
   while($row = mysql_fetch_assoc($result)){
    $line_item=$row['line_item']; ?>  
    <td><span class="text"><?=$row['line_item']?></td>  
    <td><span class="text"><?=$row['order_n']?></td>
    <td>
        <form action="">
            <select name="Description"  id="<?php echo $line_item; ?>" c
     class = "descrip_1"
                <option value="">Select a Item:</option>
                <option value="2x8-08 SYP #2">2x8-08 SYP #2</option>
                <option value="3/4 Galvanized Nut">3/4 Galvanized Nut</option>
            </select>
        </form>
    </td>
    <td width=60px><label class="results"></label></td>
    <td width=60px><label class="<?php echo $line_item; ?>"></label></td>    
</tr><?php
            } ?>
</tbody>
</table>
</div>
</body>
</html>
<script type="text/javascript">
 $(function() {
  function_1();
   $('.descrip_1').on('change', function() {
    function_2(this);
    });
 });
 function function_2(elem) {
 var $container = $(elem).parent().parent();
 var descrip_2= $container.find('.descrip_1').val();
   $.ajax({
            type: 'POST',
            data: ({p : descrip_2}),  
            url: 'cost.php',
            success: function(data) {
            // Using "results will get cost on every row,
            // but the cost is the same on each row.
            // Should be different depending on line number
            $('.results').html(data);
            //Using php echo $line_item will work only one line.
            $('.<?php echo $line_item; ?>').html(data);             }
       });
  }
 function function_1(){
    }
  </script>

PHP 成本.php文件获取成本

 <?php
 include("*****");
 $name=$_POST['p'];
 $sql = mysql_query("SELECT * FROM types WHERE Description = '".$name."'");
 $row = mysql_fetch_array($sql);
echo "<table border='0' cellspacing='0'>
<th><font color='black'>".$row['Cost']."</th>
</table>"; ?>

不需要使用变量类名。

只需使用<td width=60px><label class="line_item"></label></td>,然后

$container.find(".line_item").html(data);

在你的jQuery成功。

让我知道如果这不是您想要的,您的问题有点令人困惑(您希望更新所有行还是仅更新更改的行?这应该只更改更改的行)。

编辑

好的,使用$(elem).closest("tr").find(".line_item").html(data);

这是一个与相应的 html 松散耦合的解决方案。我错误地认为$container变量是一个容器。

Josh DM回答了我的问题,它现在可以工作了。 这是我所做的

.html

    <td width=60px><label class="line_item"></label></td>

JavaScript

<script type="text/javascript">
  $(function() {
   function_1();

    $('.descrip_1').on('change', function() {
       function_2(this);
       });  
      });
      function function_2(elem) {
// This will give the tr of the Element Which was changed

     var $container = $(elem).parent().parent();

   var descrip_2= $container.find('.descrip_1').val();

 $.ajax({
            type: 'POST',
            data: ({p : descrip_2}),    
            url: 'listercust.php',
            success: function(data) {

 $(elem).closest("tr").find(".line_item").html(data);

 }

    });
     }
   function function_1(){     
   }
  </script>