jQuery / json / Ajax - 如何从数据成功中找到第一个响应

时间:2016-07-04 11:11:51

标签: javascript jquery json ajax

这是我的完整代码:

$.ajax({
    url: 'CheckColorPrice.php',
    type: 'POST',
    data: {
        url: '<?php echo $LINK;?>',
        ColorId: ColorNumber,
        ProductUrl: '<?PHP echo $ProductUrlWithoutCode;?>'
    },
    dataType: 'json',
    success: function (data) {
            $('.Stock').hide();
            $("#PRICE").text("£ " + GetPriceForChildren(data["price"]));
            var PoundPrice = GetPriceForChildren(data["price"]);
            var lev = <?PHP echo $PoundCurrency; ?> * GetPriceForChildren(data["price"]);
            lev = lev.toFixed(2);
            $("#lev").text(lev + " лв.");
            Prices = GetPriceForChildren(data["price"]);
            PreOrder = data.preorder;
            if(PreOrder != "") {
                $("#PlaceToCart").hide();
                $("#PreOrderCart").show();
                $("#PreOrderSpan").show();
                $("#AdditionalPurchaseInfo").html("<span id='PreOrderSpan'><b style='color: #DF0303;'>Този продукт е със статус Pre-Order и не може да бъде поръчан преди:</b> <b>" + data.preorder + "</b><b>.<br><a href=/pomosht target=_blank> Повече информация за Pre-Order продукти.</a></b></span>");
                $('.Stock').hide();
            }
            if(PreOrder == "") {
                $("#PreOrderCart").hide();
                $("#PlaceToCart").show();
                $("#PreOrderSpan").hide();
            }                                   
            levs = lev;
            $("#PRODUCTPRICE").val(lev);
            $("#POUNDPRICE").val(PoundPrice);
            var AutoIncreasment = 0;
            $( ".LittleThumbs" ).remove();
            $( ".FancyThumbs" ).remove();
            $('.SizeButtons').remove();

            $.each(data.thumbnails, function(index, thumbnails) {  


                $('#ThumbnailHolder').append('<a rel="example_group" title="<?PHP echo $name;?>" class="FancyBox FancyThumbs" href="http://images.sportsdirect.com/images/products/' + thumbnails + '"><img src="http://images.sportsdirect.com/images/products/' + thumbnails + '" class="LittleThumbs" BigImageLink="http://images.sportsdirect.com/images/products/' + thumbnails + '"></a>');


            });

            $.each(data.sizenames, function(index, sizenames) {                                     
            $('.SizeButtonsHolder').append('<div class="SizeButtons" data-title="'+ sizenames +'" data-number="'+ (AutoIncreasment++) +'">'+ sizenames +'</div>');  
            });

            $('.ColorImagesNOColor').fadeOut();
            $("#Broi").val("1");
            $('#LoadingImage').hide();

    }
});

CheckColorPrice.php的内容:

<?PHP
$url = $_POST['url'];
$ColorId = $_POST['ColorId'];
$ProductUrl = $_POST['ProductUrl'];
$SizeNumber = $_POST['SizeNumber'];     

$url = "$ProductUrl?colcode=$ColorId"; 
if(isset($_POST['url']))
{   
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_COOKIE, "ChosenSite=www; SportsDirect_AnonymousUserCurrency=GBP; language=en-GB");
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSLVERSION, 3);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
        curl_setopt($curl, CURLOPT_VERBOSE, true);
        $str = curl_exec($curl);  
        curl_close($curl);  


        libxml_use_internal_errors(true); 
        $doc = new DOMDocument();
        $doc->loadHTML($str);

        $xpath = new DOMXpath($doc);

    $DataVariants = $xpath->query('//span[@class="ImgButWrap"]/@data-variants')->item(0)->nodeValue;

    $jsonStart = strpos($DataVariants, '[');
    $jsonEnd = strrpos($DataVariants, ']');

    $collections = json_decode(substr($DataVariants, $jsonStart, $jsonEnd - $jsonStart + 1));   

    $result = array();
    $SizeNames = array();

    $t  = $xpath->query('//ul[@id="piThumbList"]//li//a/img');

    foreach ($t as $Sentry) {
        $Thumbs = $Sentry->getAttribute("src");
        $FindT = array('http://images.sportsdirect.com/images/products/','_piat_','_pit.jpg');
        $RemT   = array('','_l_','_l.jpg');

        $BigImageLink = str_replace($FindT, $RemT, $Thumbs);
        $result['thumbnails'][] = $BigImageLink;
    }

    foreach ($collections as $item) {
        $ColVarId = $item->ColVarId;            

        if ($ColVarId == $ColorId){
        $result['preorder'][0] = $item->PreOrderAvailableDate;      
        foreach ($item->SizeVariants as $size) {
            $result['sizenames'][] = $size->SizeName;

            $SellPrice[] = $size->ProdSizePrices->SellPrice;    
            $Sprice = $SellPrice[0];
                $healthy2 = array('£',' ','Â');
                $yummy2   = array('','','');
                $price = str_replace($healthy2, $yummy2, $Sprice);

            $result['price'][0] = $price;

        }   
    }       

    }

    echo json_encode($result);

}   

?>

我想知道的是这部分:

$.each(data.thumbnails, function(index, thumbnails) {  

    $('#ThumbnailHolder').append('<a rel="example_group" title="<?PHP echo $name;?>" class="FancyBox FancyThumbs" href="http://images.sportsdirect.com/images/products/' + thumbnails + '"><img src="http://images.sportsdirect.com/images/products/' + thumbnails + '" class="LittleThumbs" BigImageLink="http://images.sportsdirect.com/images/products/' + thumbnails + '"></a>');


});

如何选择第一个响应并运行alert('This is the first response!');,然后完成所有其他操作。

亲切的问候。

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题:

<div id="num-1">
  <input type="checkbox" name="check-1" value="check-1"> Checkbox "check-1" of the Div "num-1" <br>
</div>

<div id="num-2">
  <input type="checkbox" name="check-1" value="check-1"> Checkbox "check-2" of the Div "num-2" <br>
</div>