Ajax为同一个html类多次调用,这个类就像这个[]

时间:2017-12-11 17:25:48

标签: php html ajax

我的代码在这里

  <table    id="tb" width="100% style="float:left; overflow:auto">
    <tr >
        <th width="300px" style=" text-align: left; margin-left: 15px">Item</th>
                    <th width="300px" style=" text-align: left; margin-left: 15px">Description</th>
        <th width="100px" style="font-size: 60%">Average<br> Price</th>

        <th width="150px">Quantity</th>
        <th width="150px">Price</th>
        <th width="150px">Discount</th>
        <th width="200px">Amount</th>
        <th width="50px"></th>
        <th></th>
    </tr>

    <tr style="height: 35px">


        <td><select style="font-size: 100% ;padding: 0 0 0 0; height: 30px; display: inline-block; width: 100%" ntype="text" 
                                        class="form-control item" name="item[]" required  >
                                            <?php $this->load->view('f-select-item'); ?></td>

        <td><input style="font-size: 100% ;padding-left: 10px; height: 30px; display: inline-block; width: 100%" ntype="text" 
                                        class="form-control" name="item_note[]"  /></td>
                                                                                    <td class="priced"><input 
                                                                                    style="height: 30px; text-align: right" pattern="[0-9]+"  value=""
                                        maxlength="10" minlength="0"  min='0' max='50000000' type="text" name="price1[]"  class="form-control ppp" /></td>


        <td><input style="height: 30px;text-align: right"  id ="num" maxlength="10" minlength="0"  pattern="[0-9]+" type="text" name="quantity[]"  class="form-control quantity"  /></td>

                                        <td><input style="height: 30px; text-align: right" pattern="[0-9]+"  maxlength="10" minlength="0"  min='0' max='50000000' type="text" name="price[]"  class="form-control price" /></td>

                                        <td><input style="height: 30px;text-align: right" pattern="[0-9]+" maxlength="10" minlength="0"  type="text" name="discount[]"  class="form-control discount"  /></td>
                                        <td><input  style="height: 30px;text-align: right" type="text"  maxlength="30" minlength="0"  name="amount[]" 
                                        class="form-control amount" value="" readonly/></td>

                                        <td style="height: 25px;text-align:right"><a href="javascript:void(0);" style="text-decoration: none; font-size: 80%; padding: 0 0 0 5; font-size:18px;" id="addMore" title="Add More Row">
                <span style="font-size:80%" class="glyphicon glyphicon-plus">
                </span>
            </a><a href='javascript:void(0);'  class='remove' title="Remove Row">
                                            <span  style="padding: 0 0 0 0; text-align:right; color:lightgray" class='glyphicon glyphicon-remove'></span></a></td>

                                        <td><input id ="num" style="text-align: right" type="hidden" name="amount1[]"  class="form-control amount1" style="font-size: 0%" value="0" />
                                        </td>

    </tr>                                       
</table>

当我调用ajax新输入时,我需要的内容放在<td ="class priced">

Ajax就在这里

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
   <script type="text/javascript">
   $(document).ready(function()
   {
   $(".item").change(function()
   {
   var id=$(this).val();
   var dataString = 'id='+ id;

   $.ajax
  ({

  type: "POST",
  url: "pp",
  data: dataString,
  cache: false,
  success: function(html)
  {

  $(".priced").html(html);
   } 
  });

  });

   });
 </script>

和ajax称为文件,这是'pp';

  <?php
  // Database Connection
  $username = 'root';
  $password = '';
  $db = "acccccc";
  try {
  $conn = new PDO('mysql:host=mbhost;dbname='.$db.'', $username, $password);
   $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   } catch(PDOException $e) {
   echo 'ERROR: ' . $e->getMessage();
    } 


    /////////////////////////////////////////////////////////////////////////////////////////////////

   if($_POST['id']){
   $id= $_POST['id'];
   $sql=$conn->prepare("select * from items where  '".$id."' in (c_v_id, item_id) ");
   $sql->execute();
   if($sql->rowCount() > 0){

                       $sum =     $amount= $q1 = $q2 = $am = $am1 = $am2 = $sumq =0;

                    while($row=$sql->fetch(PDO::FETCH_ASSOC)){




                        if($id == $row['c_v_id']){

                        $am = $row['amount'];
                        $q1 = $row['quantity'];

                        if($am != 0 and $q1 != 0 ){
                 $amount = $row['amount']/$row['quantity'];

                        } else{ $amount = 0;}
                        }elseif($id == $row['item_id']){

                            $am2 = $row['amount'];
                             $q2 = $row['quantity'];
                            if($am2 != 0 and $q2 != 0 ){
                            $amount = -$row['amount']/$row['quantity'];
                        }else{ $amount = 0;}
                        }
                    $sum += $amount;
                    $sumq = $q1 - $q2;
                    }
                    ?>
                    <input  title="<?php echo 'Available Quantity: '.$sumq; ?>" style="height: 30px; text-align: right" pattern="[0-9]+"  value="<?php echo $sum; ?>"
                                        maxlength="10" minlength="0"  min='0' max='50000000' type="text" name="price1[]"  class="form-control ppp" />

                    <?php 

     }else{ 


?>

<input  title="Zero Quantity" style="height: 30px; text-align: right" pattern="[0-9]+"  value="0"
                                        maxlength="10" minlength="0"  min='0' max='50000000' type="text" name="price1[]"  class="form-control ppp" />

                    <?php }


  }




 ?>

此代码可以一次性调用...

td在jquet动态添加行之后是[]的多次,所以我该怎么做..每个td应该有单独的响应...这次我调用每个td但是响应是同样适用于所有tds ..

请提出答案......提前谢谢。

2 个答案:

答案 0 :(得分:1)

你不应该在循环中进行ajax调用,你应该在上一次调用成功之后但在setTimeout函数中进行每次调用。这是一个代码结构,它将以递归方式执行,并应解决您的问题。你自己必须决定结束递归函数的条件:

function myAjaxCall(){
    $.ajax
({

type: "POST",
url: "pp",
data: dataString,
cache: false,
success: function(html)
{
$(".priced").html(html);

if(!/* the condition for Last ajax call*/)
  setTimeout(myAjaxCall,0);
} 
}); 

}

请注意,您的代码的另一个问题可能是您在每次调用后替换html。使用$(".priced").html(html);可能更好,而不是$(".priced").append(html)

答案 1 :(得分:0)

尝试使用jquery.on()。 Wordpress正在使用此功能。 reed the doc: - http://api.jquery.com/on/ 用法: -

$(document).on('click','.ajax-loaded-content',function(){ myfunc();})