2014-06-28 32 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

相关问题