2014-06-28 35 views
0

我得到錯誤PriceSelling未定義。但事實上,我知道它在頁面上,因爲它將它記錄在控制檯中。請幫忙!謝謝PriceSelling未定義。但是,它是

$.get(window.location, function(data){ 
    var regex=/<span class="it " data-se="item-privatesale-price">([\d,]+)<\/span>/; 
    var PriceSelling = data.match(regex)[1]; 
    console.log(PriceSelling); 
}); 

function get(name){ 
    if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search)) 
     return decodeURIComponent(name[1]); 
} 

if (get('bot') && get('expecting') && get('expecting') == PriceSelling) { 
console.log("It's a go!"); 
document.getElementsByClassName('conf-buy-now btn-primary btn-medium PurchaseButton ')[0].click(); 
//document.getElementById('conf-confirm-btn').click(); 
}; 
+1

PriceSelling未在您調用的範圍內定義。 – smk

回答

0

它被定義在傳遞給$.get的回調函數的範圍內。

但它沒有在全球範圍內定義。

你可以做

var PriceSelling; 

$.get(window.location, function(data){ 
    var regex=/<span class="it " data-se="item-privatesale-price">([\d,]+)<\/span>/; 
    PriceSelling = data.match(regex)[1]; 
    console.log(PriceSelling); 
}); 

function get(name){ 
    if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search)) 
    return decodeURIComponent(name[1]); 
} 

if (get('bot') && get('expecting') && get('expecting') == PriceSelling) { 
    console.log("It's a go!"); 
    document.getElementsByClassName('conf-buy-now btn-primary btn-medium PurchaseButton ')[0].click(); 
    //document.getElementById('conf-confirm-btn').click(); 
} 

,但同時你也不會得到ReferenceError,它不會有太大的好,因爲PriceSelling將永遠是undefined

但我注意到您正嘗試立即使用該響應。你必須在回調中使用它,一旦收到響應就會調用它。

您可能會受益於How do I return the response from an asynchronous call?

$.get(window.location, function(data){ 
    var regex=/<span class="it " data-se="item-privatesale-price">([\d,]+)<\/span>/; 
    var PriceSelling = data.match(regex)[1]; 
    console.log(PriceSelling); 

    function get(name){ 
    if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search)) 
     return decodeURIComponent(name[1]); 
    } 

    if (get('bot') && get('expecting') && get('expecting') == PriceSelling) { 
    console.log("It's a go!"); 
    document.getElementsByClassName('conf-buy-now btn-primary btn-medium PurchaseButton ')[0].click(); 
    //document.getElementById('conf-confirm-btn').click(); 
    } 
}); 
+0

它爲什麼記錄它呢? – user3781546

+2

它記錄,因爲PriceSelling聲明和您的console.log在相同的範圍內。 – ExWei

相關問題