没有定义PriceSelling.但是,它是

PriceSelling is not defined. But, it is

本文关键字:它是 但是 PriceSelling 定义      更新时间:2023-09-26

我得到错误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();
}; 

在传递给$.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

但是我注意到你正试图马上使用回应。您必须在回调中使用它,该回调将在收到响应后调用。

您可能会受益于如何从异步调用返回响应?

$.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();
  } 
});
相关文章: