0

出来上がったクラスを作ったのですが、1回でもいいのに3回もDOMに浸しているので効率が悪いと思います。このために残りのコードを見る必要はないと思うので、整理するために非効率的な部分だけを投稿します。

function showSelectedAttr(){

    var productID = <?php echo $product_info['products_id'];?>;
    var sizeID = 0; 
    var tallID = 0;
    var colID = 0;

    $(".sizeopt").each(function() {
          if ($(this).is(':checked'))  sizeID = $(this).val();                         
     });

     $(".tallopt").each(function() {
          if ($(this).is(':checked'))  tallID = $(this).val();                         
     });

     $(".mine_color").each(function() {
          if ($(this).is(':checked'))  colID = $(this).val();                          
     });

    $.ajax({
              type: "POST",
              url: 'get_product_attribute.php',
              data: "product_id="+ productID +"&size_id="+ sizeID,
              success: function( response ) {       
                    $("#attr_container").html(response);
              } 
     });

     $.ajax({
              type: "POST",
              url: 'get_product_attribute.php',
              data: "product_id="+ productID +"&size_id="+ tallID,
              success: function( response ) {       
                    $("#attr_container_tall").html(response);
              } 
     });

         $.ajax({
              type: "POST",
              url: 'get_product_attribute.php',
              data: "product_id="+ productID +"&size_id="+ colID,
              success: function( response ) {       
                    $("#attr_container_color").html(response);
              } 
     });

}

ご覧のとおり、ajax API は 3 回別々に呼び出されます。これを行うより良い方法はありますか?

4

1 に答える 1

1
function showSelectedAttr() {

    var productID = <?php echo $product_info['products_id'] ?>;
    var sizeID = 0; 
    var tallID = 0;
    var colID = 0;

    $(".sizeopt").each(function() {
        if ($(this).is(':checked')) sizeID = $(this).val();                         
    });

    $(".tallopt").each(function() {
         if ($(this).is(':checked')) tallID = $(this).val();                         
    });

    $(".mine_color").each(function() {
         if ($(this).is(':checked')) colID = $(this).val();                          
    });

    $.ajax({ 
        dataType:"json",
        type: "POST", 
        url: 'get_product_attribute.php', 
        data: {
            productId : productID,
            sizeId : sizeID,
            tailId : tailID,
            colId : colID
        }, 
        success: function( response ) {    
            $("#attr_container").html(response.Text);
            $("#attr_container_tall").html(response.Tall);
            $("#attr_container_color").html(response.Color);
        }  
    }); 
}

レスポンスはjson形式です{Text: "value", Tall: "value", Color: "value" }

于 2012-06-20T02:56:10.763 に答える