如何在ajax调用的迭代ID中获得结果

时间:2018-08-08 05:27:27

标签: javascript php ajax

如何在ajax调用中的迭代ID中获取结果...结果应该在迭代ID中,但是...结果不显示。如何在php ajax中获取dsplay结果以for循环迭代id? >

这是我的html代码

<tr>
                      <td><span id="sr_no">1</span></td>
                      <td><select type="text" name="item_name[]" id="item_name1" class="form-control input-sm"  onChange="getbrand(this.value);" />
                      <option value=""></option>
                      <?php
    include "conn.php";
    $sql = "SELECT category FROM `product` GROUP BY category";
    $run = mysqli_query($conn,$sql);

    while($row=mysqli_fetch_array($run))
    {
    ?>
<option><?php echo $row['category']; ?></option>
        <?php } ?>
                      </select>
                      </td>
                      <td>
                        <select type="text" name="item_name[]" id="brand" class="form-control input-sm" />
                        <option value=""></option>

                        </select>
                      </td>
                      <td><input type="text" name="order_item_quantity[]" id="order_item_quantity1" data-srno="1" class="form-control input-sm order_item_quantity" /></td>
                      <td><input type="text" name="order_item_price[]" id="order_item_price1" data-srno="1" class="form-control input-sm number_only order_item_price" /></td>
                      <td><input type="text" name="order_item_actual_amount[]" id="order_item_actual_amount1" data-srno="1" class="form-control input-sm order_item_actual_amount" readonly /></td>
                      <td><input type="text" name="order_item_tax1_rate[]" id="order_item_tax1_rate1" data-srno="1" class="form-control input-sm number_only order_item_tax1_rate" /></td>
                      <td><input type="text" name="order_item_tax1_amount[]" id="order_item_tax1_amount1" data-srno="1" readonly class="form-control input-sm order_item_tax1_amount" /></td>
                     <td><input type="text" name="order_item_final_amount[]" id="order_item_final_amount1" data-srno="1" readonly class="form-control input-sm order_item_final_amount" /></td>
                      <td></td>
                    </tr>
                  </table>
                  <div align="right">
                    <button type="button" name="add_row" id="add_row" class="btn btn-success btn-xs">+</button>
                  </div>
                </td>
              </tr>

这是我的Java脚本

<script>
      $(document).ready(function(){
        var final_total_amt = $('#final_total_amt').text();
        var count = 1;

        $(document).on('click', '#add_row', function(){
          count++;
          $('#total_item').val(count);
          var html_code = '';
          html_code += '<tr id="row_id_'+count+'">';
          html_code += '<td><span id="sr_no">'+count+'</span></td>';

         html_code += '<td><select name="item_name[]" id="item_name'+count+'" class="form-control input-sm" onChange="getbrand(this.value);" ><option></option><?php $sql='Select category from product GROUP BY category'; $run=mysqli_query($conn,$sql); while($row=mysqli_fetch_array($run)){ echo '<option>'.$row['category'].'</option>'; } ?></select></td>';

          html_code += '<td><select name="brand[]" id="brand'+count+'" class="form-control input-sm" ><option></option></select></td>';

          html_code += '<td><input type="text" name="order_item_quantity[]" id="order_item_quantity'+count+'" data-srno="'+count+'" class="form-control input-sm number_only order_item_quantity" /></td>';

          html_code += '<td><input type="text" name="order_item_price[]" id="order_item_price'+count+'" data-srno="'+count+'" class="form-control input-sm number_only order_item_price" /></td>';

          html_code += '<td><input type="text" name="order_item_actual_amount[]" id="order_item_actual_amount'+count+'" data-srno="'+count+'" class="form-control input-sm order_item_actual_amount" readonly /></td>';

          html_code += '<td><input type="text" name="order_item_tax1_rate[]" id="order_item_tax1_rate'+count+'" data-srno="'+count+'" class="form-control input-sm number_only order_item_tax1_rate" /></td>';

          html_code += '<td><input type="text" name="order_item_tax1_amount[]" id="order_item_tax1_amount'+count+'" data-srno="'+count+'" readonly class="form-control input-sm order_item_tax1_amount" /></td>';

          html_code += '<td><input type="text" name="order_item_final_amount[]" id="order_item_final_amount'+count+'" data-srno="'+count+'" readonly class="form-control input-sm order_item_final_amount" /></td>';

          html_code += '<td><button type="button" name="remove_row" id="'+count+'" class="btn btn-danger btn-xs remove_row">X</button></td>';
          html_code += '</tr>';
          $('#invoice-item-table').append(html_code);
        });

        $(document).on('click', '.remove_row', function(){
          var row_id = $(this).attr("id");
          var total_item_amount = $('#order_item_final_amount'+row_id).val();
          var final_amount = $('#final_total_amt').text();
          var result_amount = parseFloat(final_amount) - parseFloat(total_item_amount);
          $('#final_total_amt').text(result_amount);
          $('#row_id_'+row_id).remove();
          count--;
          $('#total_item').val(count);
        });


function getbrand(count) {
 for(k=1; k<=count; k++)
          {
$.ajax({
type: "POST",
url: "ajax_brand.php",
data:'state_id='+val,
success: function(data){ 

$("#brand"+k).html(data);
}
});}
}
</script>

我对此脚本的问题.....如何在此ajax调用中为id添加迭代。

function getbrand(count) {
     for(k=1; k<=count; k++)
              {


     $.ajax({
        type: "POST",
        url: "ajax_brand.php",
        data:'state_id='+val,
        success: function(data){ 

        $("#brand"+k).html(data);
        }
        });}
        }

2 个答案:

答案 0 :(得分:0)

由于我们没有关于如何从ajax_brand.php返回数据的格式,因此我认为这是品牌ID和品牌名称条目的数组。

例如

[
   {
      "id":"1",
      "name":"Brand 1"
   },
   {
      "id":"2",
      "name":"Brand 2"
   }
]

然后,ajax的成功应该是这样的。当然,您需要用for循环brand1计数器替换ID k

$(document).ready(function() {
  $('#button-fire').on('click', function() {
    handleFire();
  });
});



function handleFire() {

  var brandSelect = $('#brand1').empty();

  $.ajax({
    type: "POST",
    // mock url
    url: "http://5b6a940cad81cd0014d152d2.mockapi.io/brand/api/v1/brands",
    success: function(data) {
      data.forEach(function(brand) {
        brandSelect.append($('<option>', {
            value: brand.id
          }).html(brand.name)

        );
      });

    }
  });

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="brand[]" id="brand1" class="form-control input-sm">
  <option></option>
</select>



<button id="button-fire">fire</button>

答案 1 :(得分:0)

这是我的ajax品牌调用,使用功能

从数据库中显示
 function getprods(val)
{
 for(j=1; j<=count; j++)
{
  var itemp = $('#item_code'+j).val();
  $.ajax({
url: 'ajax_getproduct2.php',
type: 'POST',
data: 'state_id='+itemp,
dataType: 'json',
success:function(data){
var len = data.length;
if(len > 0){
var id = data[0]['id'];
var item = data[0]['item'];
var brand = data[0]['brand'];

$('#brand'+j).val(brand);


}

}
});
}
}

但是我无法获得品牌价值