2015-09-25 203 views
2

實際上,我寫了一段代碼來檢測地理位置,並根據該地理位置貨幣會改變,它也在工作,但這裏的問題是一次又一次,這段代碼刷新整個頁面,如果我刪除其他部分網頁正在刷新如果我繼續說別的條件不工作,可以在任何一個建議我如何停止,這是我寫的如何停止頁面的刷新?

<script type="text/javascript"> 
    //alert("Welcome to our visitors from "+geoplugin_currencyCode()); 
    alert("Welcome to our visitors from "+geoplugin_countryName()); 

    $flag=false; 
    if($flag===false) { 
     var country = geoplugin_countryName(); 

     if(country === 'India') { 
      $('#currency input[name=\'code\']').attr('value', 'INR'); 
      $('#currency').submit(); 
      alert('Your currency is set to INR'); 
     } else { 
      $('#currency input[name=\'code\']').attr('value', 'USD'); 
      $("#currency").change(); 
      alert('Your currency is set to USD'); 
     } 
    } else { 
     return false; 
    } 
</script> 
+0

我試過使用你的代碼 - 我的peage沒有自動重新加載。哪些代碼會刷新你的頁面? – Erminesoft

+0

你是否刪除了其他部分如果不刪除它並嘗試在我的頁面中重新加載iam在opencart中使用它 – martin

+0

你有更多的其他...我假設你的意思是包含'return false; else的else正確? –

回答

1

嘗試

$(function(){ 
    alert("Welcome to our visitors from "+geoplugin_countryName()); 
    $flag=false; 
    if($flag===false){ 
     var country = geoplugin_countryName(); 
     if(country === "India"){ 
      $("#currency input[name=\'code\']").attr("value", "INR"); 
      $("#currency").submit(); 
      alert("Your currency is set to INR"); 
      return false; 
     } else{ 
      $("#currency input[name=\'code\']").attr("value", "USD"); 
      $("#currency").change(); 
      alert("Your currency is set to USD"); 
      return false; 
     } 
    } else{ 
     return false; 
    } 
    }); 
+0

不能繼續爲我重新加載 – martin

+0

你能說 - 在你的代碼中有什麼值'國家'變量? – Erminesoft

+0

實際上這是我用來取得國家名稱的插件 martin

0

你的代碼是好的,但你必須以不同的方式處理它。首先在頁面加載時,假設您的國家爲India。所以現在是if(country === "India"){代碼,它執行$("#currency").submit();行並提交表單。所以一旦提交表單,您必須設置INR值可能在隱藏字段中。然後,您可以添加一行代碼來驗證隱藏字段是否具有INR值。如果它的值爲INR則不要執行代碼。

假設你有隱藏字段,編號爲currency_code。因此,現在表單提交後,您從$("#currency input[name=\'code\']")發佈的任何值只需在#currency_code輸入隱藏字段中設置該值即可。

現在檢查您的代碼中的條件。

<script type="text/javascript"> 
    //alert("Welcome to our visitors from "+geoplugin_currencyCode()); 
    alert("Welcome to our visitors from "+geoplugin_countryName()); 

    $flag=false; 
    if($flag===false) { 
     var country = geoplugin_countryName(); 

     if(country === 'India') { 
      if($('#currency_code') !== 'INR') { 
       $('#currency input[name=\'code\']').attr('value', 'INR'); 
       $('#currency').submit(); 
       alert('Your currency is set to INR'); 
      } else { 
       alert('Your currency is already set to INR'); 
      } 
     } else { 
      $('#currency input[name=\'code\']').attr('value', 'USD'); 
      $("#currency").change(); 
      alert('Your currency is set to USD'); 
     } 
    } else { 
     return false; 
    } 
</script> 

我不確定您的所有HTML代碼。但這是你可以處理你的問題的方式。希望這會幫助你。