首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Ajax多次调用相同的html类,其数组中如下所示[]

Ajax多次调用相同的html类,其数组中如下所示[]
EN

Stack Overflow用户
提问于 2017-12-12 01:25:48
回答 2查看 55关注 0票数 0

我的代码在这里

代码语言:javascript
运行
复制
  <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在这里

代码语言:javascript
运行
复制
    <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';

代码语言:javascript
运行
复制
  <?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在动态添加行之后会多次出现[],所以我应该怎么做呢?每个td都应该有单独的响应...这次我调用了每个td,但所有td的响应都是相同的。

请回答我..。提前谢谢。

EN

回答 2

Stack Overflow用户

发布于 2017-12-12 01:56:38

不应该在循环中进行ajax调用,应该在前一次调用成功之后进行每次调用,而应该在setTimeout函数中进行调用。这是一个代码结构,它将递归地完成这项工作,并且应该可以解决您的问题。您必须自己决定结束递归函数的条件:

代码语言:javascript
运行
复制
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").append(html)可能比使用$(".priced").html(html);更好。

票数 1
EN

Stack Overflow用户

发布于 2017-12-12 02:05:55

尝试使用jquery.on()。Wordpress正在使用这个函数。读取文档:- http://api.jquery.com/on/用法:-

代码语言:javascript
运行
复制
$(document).on('click','.ajax-loaded-content',function(){ myfunc();})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47758153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档