我有以下工作的这个功能。我将一个名为“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>');
});
}
}
答案 0 :(得分:1)
问题出在这一行:
$j('#qtyUpdateBox').append('<a href="#" class="updatePriceButton"
onclick="updatePrices('+IDs+')">Update</a>')
这会将IDs
转换为字符串,以便在该属性中使用。 (例如,尝试console.log("anteater " + IDs);
)。你应该以编程方式设置一个onclick监听器。
$("#newElement").click(function() {
updatePrices(IDs);
});