申し訳ありませんが、私は今一日以上頭を傷つけています。そして、私はそれが解決するのがとても簡単だと確信しています。JSは特定の行の後でコードの処理を停止するのはなぜですか?
以下の関数は、phpファイルからいくつかの投稿データを取得します(xml形式が返されます)。成功すると、カート内の項目がこの項目に既に存在するかどうかをチェックして同じ名前で2回追加されています。したがって、アイテムの現在のリストをループし、一致するものを見つけます。それが見つからない場合は、項目を上手く追加します。存在する場合は、その行を新しい金額で更新する操作が必要です。
いくつかの奇妙な理由により、一致があるとすぐにコードを実行することができません。 if(match == true)
は、console.log("index found present status: "+match);
がコンソールのtrue
を返すにもかかわらず、何もしません。
これはどのように可能ですか?スコープなどと関係がありますか?
は、そこにある関数の2番目の呼び出しに、重複して1つのアイテムにマージする必要があることを確認するためだけにあります。毎回ウェブサイト上のものをクリックして保存します。
function addToCart (ueid, amount)
{
// you can remove the follow alert() call, typically this function would
// scroll (through an #anchor or maybe tween/scroll with jQuery) down to
// the area below the panorama tour and there display the user-interface
// for buying/sponsoring items
console.log("buy "+ueid+" "+amount+" times");
var match = false;
console.log("before ajax present status: "+match); //output: false
//Get detailed info about this item
iteminfo = jQuery.ajax(iteminfourl, { data: { "ueid": ueid }, type: "POST",
success:
function(data)
{
console.log("ajax success present status: "+match); //output: false
totalprice = (amount * $(data).find('price').text());
//first check if the item already exists in the table
currentContents = basket.fnGetData();
if(currentContents.length > 0)
{
for(index = 0; index <= currentContents.length; index++)
{
if(currentContents[index][0] == ueid)
{
console.log("ueid and index are the same");
match = true;
var updateIndex = index;
console.log("index found present status: "+match); //output: true
}
}
}
if(match == true)
{
//this never gets executed, even if match is set to true.
//also console.logs just before the if statement are only
//executed when match = false all the way...
console.log("item is present, update index "+updateIndex);
//basket.fnUpdate()
}
else
{
basket.fnAddData(
[
ueid,
amount,
totalprice,
'X'
]
);
if(basket.fnGetData().length == 1)
addToCart('window', 1);
}
}
});
}
なぜiteminfo = jQuery.ajax()?? – DrStrangeLove
は予約済みの世界ですので、* does_it_match *のようなsthgに変更して再試行することができます。 – hornetbzz