0

我在下面有这个功能。我将一个名为“IDs”的数组投射到函数中,并且在第一个“if”语句中没有问题,但是当您输入“else”语句时,“IDs”等于一个整数。你可以在 else 块开始后的 console.log 注释中看到我的意思。你能明白为什么“ID”不再是一个数组吗?

将参数转换为函数的位置(请参阅 console.log “是 ID 的数组吗?”):

$j('select').change(function(e) { //on changing the attribute input
        if ($j(this).find('option:selected')[0].text=='Choose an Option...')//see if there is a value in the dropdown
        { 
            $j('#vendorsButton').removeClass().addClass('vendorsButtonOff'); //Gray out the button and disable
            $j("#vendorBox").hide();
        }
        else
        {
            $j('#vendorsButton').removeClass().addClass('vendorsButtonOn'); //Enable and make it gold


            var items = $j('#attribute136').children().length;//for the length of the select list (children of select)
            //console.log("There are " + items + " children in select parent.");
            var IDs = []; //collect all of the vendor ids in an array
            for (var i=2; i <= items; i++)
            {
                var text= $j('#attribute136 option:nth-child(' + i + ')').text();//get text of option
                var value= $j('#attribute136 option:nth-child(' + i + ')').val();//value of option for add to cart function
                console.log('Value to text is ' + value + ' : ' + text); 
                //console.log(text);
                IDs[i-2]= value;
            } 
            console.log('Is IDs an array? ' + IDs); //returns "5,3"
            //insert quantity box
            $j('#vendorBox').append("<div id='qtyUpdateBox'></div");
            $j('#qtyUpdateBox').append('<label for="qty">Qty:</label>'); 
            $j('#qtyUpdateBox').append('<input type="text" name="qty" id="qty" maxlength="12" value="1" title="Qty" class="input-text qty" />');
            $j('#qtyUpdateBox').append('<a href="#" class="updatePriceButton" onclick="updatePrices('+IDs+')">Update</a>');
            //populate vendorBox
            $j('.vendorList').empty().removeClass('vendorList');
            $j('#vendorBox').append("<ul class='vendorList'></ul>");//make list 
            vendorInfo(IDs);
        }

    });

参数被转换到的第一个函数(这次被调用,它工作得很好):

function vendorInfo(IDs){//AJAX CALL TO PHP SCRIPT TO OBTAIN VENDOR APPLICABLE VALUES

        $j.ajax({
            type: "POST",
            url: "/ajax_calls/vendorInfo.php",
            //dataType:"json",
            data: { 'vendorID': IDs} //if passing array doesn't work try to construct json object
            }).done(function(data) {
                var data= JSON.parse(data);
                //console.log('success');
                //console.log('The data is for vendorinfo ');
                //console.log(data);
                for(var i=0; i < IDs.length; i++)
                {
                    //console.log('Data for ' + data[i].id);
                    $j('.vendorList').append("<li class='vendorListItem' id='" + data[i].id + "'><ul class='details" + data[i].id + "'><li class='vendorName'>" + data[i].id+': '+data[i].name+ "</li><li class='vendorDescription'>"+ data[i].description +"</li><li class='priceBlock'></li></ul></li>");//add text to list item
                    addToCartBlock(data[i].id);
                    //displayPrice(data[i].id);
                    //instead we will call updatePrice, which will display table value prices and save applicable price when addtocart function called
                }

                    $j('.vendorList').append('<div style="clear:both"></div>');
                    updatePrices(IDs);
            }); 
    }   

当“更新”按钮被点击时参数被转换成的函数(当函数被调用时,一个整数被转换成它而不是它应该的数组):

function updatePrices(IDs){
    var product_id= <?=$product_id ?>;
    //var price = <?=$_price ?>;
    var simpleArray = <?=json_encode($simpleArray)?>;
    var qty= $j("#qtyUpdateBox input").val();
    var colorSelected = $j("#attribute92 option:selected").val();
    //var IDs = IDs;

    if (qty==1){
        for(i=0; i<IDs.length; i++)
        {   
            var vendor = IDs[i]; 
            //CAITLIN you are going to need way to search for other attributes, GET list of attributes
            $j('.details'+vendor+ ' .priceBlock').append('<span>'+simpleArray[vendor][colorSelected]+'</span>');
        }
    }
    else{
        console.log('The Ids are ' + IDs.length); //equates to undefined
        console.log('The Ids are ' + IDs); //equates to "5"
        console.log('The Ids are ' + IDs[1]); //equates to undefined
        //ajax call to obtain tier prices for each vendor id
        $j.ajax({
                type: "POST",
                url: "/ajax_calls/updatePrices.php",
                data: { 'vendorID': IDs, 'product_id': product_id}
                }).done(function(data,IDs,simpleArray) {
                    var data= JSON.parse(data);
                    console.log('Range start is ' + data.tier2_range_start);
                    for(i=0; i<IDs.length; i++)
                    {   
                        var vendor = IDs[i]; 
                        console.log('The vendor is ' + vendor[1]);
                        var basePrice = simpleArray[vendor][colorSelected];

                        if (qty < data.tier2_range_start){
                            simpleArray[vendor][colorSelected]= basePrice * qty;
                        }
                        else if (qty > data.tier2_range_start){
                            simpleArray[vendor][colorSelected]= (basePrice * qty) * tier2_discount;
                        }
                        else if (qty > data.tier3_range_start){
                            simpleArray[vendor][colorSelected]= (basePrice * qty) * tier3_discount;
                        }
                        else if (qty > data.tier4_range_start){
                            simpleArray[vendor][colorSelected]= (basePrice * qty) * tier4_discount;
                        }
                        else if (qty > data.tier5_range_start){
                            simpleArray[vendor][colorSelected]= (basePrice * qty) * tier5_discount;
                        }
                        else{
                            console.log('Something went wrong');
                        }

                    }
                    $j('.details'+vendor+ ' .priceBlock').append('<span>'+simpleArray[vendor][colorSelected]+'</span>');
                }); 
    }
}       
4

1 回答 1

1

问题出在这一行:

$j('#qtyUpdateBox').append('<a href="#" class="updatePriceButton"     
 onclick="updatePrices('+IDs+')">Update</a>')

这将转换IDs为用于该属性的字符串。(例如,尝试console.log("anteater " + IDs);)。您可能应该以编程方式设置 onclick 侦听器。

$("#newElement").click(function() {
  updatePrices(IDs);
});
于 2013-05-27T20:58:00.520 回答