繁体   English   中英

返回 ajax 中的多个 html 数据

[英]return multiple html data in ajax

在我的 ajax 代码中

.............
........
success: function(datases) 
               {
 $.ajax({

         url:"checkfitment",
         method: 'POST',
        data: {checkfitment:checkfitment, make:selectedmakes},
        success: function(datas) 
    {
 $(".checkfirmentidmsg").html(datas); 
        }
   }); 

 $.ajax({
 url:"showattributespecification",
 method: 'POST',
 data: {checkfitment:checkfitment , make:selectedmakes},
 success: function(datas) 
        {
        $("#specificationsforms").hide();
        $("#showattributespecificationmsg").html(datas); 
             }
});
} 

以上两个 ajax 调用主 ajax 成功结果,两个 ajax 传递相同的数据但结果不同。

功能

public function checkfitment()
{
 $make = $_POST['make'];
$fiter_products= DB::select(...............);
$countfitment = count($fiter_products);
if($countfitment > '0')
        {
?><span class="checkfit"><input type="checkbox" readonly="readonly" checked="checked" name="fitmentchecked" value="1" /> Fits <?php echo $year. ' '.$make.' '.$model;?> <div class="checkfit-oem"><?php echo  '[OEM '.$bolt_pattern.' '.$rim_width.'Jx'.$rim_diameter.']'; ?></div>

         <style> .mfp-container{ display:none;}
                       .checkFitmentsss{ display:none; position:inherit}
                       [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
    position: absolute;
    opacity:unset  !important;;
</style>

            <?php
}
}

另一个功能

public function showattributespecification()
{
 $make = $_POST['make'];
$fiter_products= DB::select(...............);
$countfitment = count($fiter_products);
if($countfitment > '0')
        {
    echo '<table class="table table-condensed">
                                    <thead>
                                        <tr class="cart_menu">
                                            <td>S.No</td>
                                            <td>Size</td>
                                            <td></td> 
                                            <td>Price</td> 

                                            <td></td> 

                                        </tr>
                                    </thead>';
            $k=1; 
            foreach($fiter_products as $rows)
            {
            ?> 
 <tr class="cart_menu" style="background: aliceblue;">
 <td><?php echo $k; ?></td>
 <td><?php echo $rows->size; ?></td>
  <td><input type="number" name="qty" id="qtys<?php echo $rows->id; ?>" value="4" min="4" /></td>

<td class=""> <?php echo $rows->price; ?> AED</td>
<?php /*?><td>
<div class="stock-btn"><a href="#!"><?php echo $rows->stock_availability; ?></a></div> 
</td><?php */?>
<td>
<?php if($rows->stock_availability=='Available') { ?>
 <div class="stock-btn avilebless" id="avilebleaddtocart<?php echo $rows->id; ?>" style="display:block;"> 
<a href="javascript:void(0)" onclick="addtocarttirebyid<?php echo $rows->id; ?>()" id="<?php echo $rows->id; ?>">Add to Cart</a>
</div>
<?php }
}
}

this time the result is show very slowly and i think its write in a single function and one ajax call,how to write these two function in a single function, how to store these data in a variable?

您可以从服务器端遵循以下方法并使用数据类型为json的以下 ajax 方法来接收多个值

$.ajax({        
    url     : "myurl.php",
    method  : 'POST',
    datatype : 'json',
    data    : {
                params1 : value1,
                params2 : value2
              },
    success : function(datas) {

      $("#elem1").html(data.html1); 
      $("#elem2").html(data.html2);

    }
}); 

myurl.php 服务器端脚本应该如下

<?php
    $return = array();
    $html1  = '';
    $html2  = '';

    $html1.='<h2>This is a test element</h2>';
    $html2.='<h2>This is another test element</h2>'

    $return['html1'] = $html1;
    $return['html2'] = $html2;

    echo json_encode($return); exit;

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM