2016-08-06 48 views
0

如何在JavaScript中將局部變量轉換爲全局變量?如何將JavaScript局部變量轉換爲全局可見的?

window.addEventListener("load", function() { 

    var fakeRadioButtonsFranType = document.querySelectorAll(".iCheck-helper"); 
    for (var i = 0; i < fakeRadioButtonsFranType.length; i++) { 
     fakeRadioButtonsFranType[i].addEventListener("click", function() { 
      fran_type = Number(this.parentNode.querySelector("input").value); 

      var dataPass = 'fran_type=' + fran_type; 
       $.ajax({ // Send the username val to available.php 
        type: 'POST', 
        data: dataPass, 
        url: '<?= site_url('Alluser/getLocations'); ?>', 
        success: function (responseText) { // Get the result 

         var allZonals = responseText; 
        } 
       }); 
     }); 
     } 
}); 

我想用allZonals變量。

+0

申報到窗口對象。 – Jain

+0

'window.allZonals = allZonals'。或者不要將它聲明爲'var',而只是使用'window.allZonals' – vlaz

+0

分配給窗口對象或在全局範圍內創建,並且只需修改 –

回答

1

你可以把它分配給全球window對象:

window.allZonals = responseText;