2017-08-16 82 views
1

我有一個小的應用程序和前端的工作看起來像以下,使用輸入值在全球範圍內使用JavaScript

enter image description here

我選擇貨幣後,Address Generate按鈕被激活,並點擊它會打開一個帶有輸入選項的模式。

enter image description here

我想有在全球範圍內的wallet name。目標網頁的代碼是在這裏,

<body id="page-top" class="index"> 

<!-- modal for providing the name of the wallet--> 
<div id="walletNameModal" class="modal fade bd-example-modal-sm" role="dialog" 
    aria-labelledby="mySmallModalLabel" aria-hidden="true" tabindex="-1"> 

    <div class="modal-dialog modal-sm" role="document"> 

     <!--layout of the set of modals--> 
     <div class="modal-content"> 
      <!--modal header--> 
      <div class="modal-header"> 
       <!--close the dialog--> 
       <button type="button" class="close" data-dismiss="modal">&times;</button> 
       <h4 class="modal-title">Wallet Name</h4> 
      </div> 

      <!--modal body--> 
      <div class="modal-body"> 
       <!--provide the wallet name as input text--> 
       <input id="addressName" type="text" class="form-control"> 
      </div> 

      <!--modal footer--> 
      <div class="modal-footer"> 
       <button id="addressNameSubmitBtn" type="button" class="btn btn-default" data-dismiss="modal">Submit 
       </button> 
      </div> 
     </div> 
    </div> 
</div> 


<section id="myform"> 

    <div class="container"> 

     <div class="row"> 
      <div class="col-lg-10"> 
       <form name="landingBox" id="walletForm" class="form-inline" novalidate> 

        <!--Select Currency--> 
        <div class="form-group controls"> 
         <select id="selectCurrency" name="" title="Select Currency" 
           class="btn-primary form-control"> 
          <option selected disabled>Select Currency</option> 
          <option value="0">Bitcoin</option> 
          <option value="1">Ethereum</option> 
          <option value="2">Litecoin</option> 
          <option value="3">Nem</option> 
          <option value="4">Ripple</option> 
          <option value="5">Dash</option> 
         </select> 
        </div> 

        <!--Generate Address--> 
        <div class="form-group controls"> 
         <button id="generateAddress" type="button" class="btn btn-primary" disabled> 
          Address Generate 
         </button> 
        </div> 

        <!--Show Balance--> 
        <div class="form-group controls"> 
         <button id="balance" type="button" class="btn btn-primary" disabled> 
          Balance 
         </button> 
        </div> 

        <!--Show Transactions --> 
        <div class="form-group controls"> 
         <button id="transactions" type="button" class="btn btn-primary" disabled> 
          Transactions 
         </button> 
        </div> 

        <!--Send Money--> 
        <div class="form-group controls"> 
         <button id="sendMoney" type="button" class="btn btn-primary" disabled> 
          Transactions 
         </button> 
        </div> 

        </br> 
        </br> 
        <!--Select Address Drop-down--> 
        <div class="form-group controls"> 
         <select name="" id="address" class="form-control input-sm"> 
          <option value=""></option> 
         </select> 
        </div> 
       </form> 

      </div> 
     </div> 
    </div> 
</section> 


</body> 

項目結構下,

enter image description here

中的JavaScript寫在main.js文件,

$(document).ready(function() { 

    var selectedCurrency, walletName; 

    // if the currency is selected, make the address generate button active 
    $('#selectCurrency').change(function() { 
     selectedCurrency = $('#selectCurrency option:selected').text(); 
     $('#generateAddress').removeAttr('disabled'); 
    }); 

    // if the address generate btn is active and clicked, open the modal 
    // to provide the wallet name 
    $('#generateAddress').click(function() { 
     $('#walletNameModal').modal(); 
    }); 

    // after the modal submission, get the wallet name 
    $('#addressNameSubmitBtn').on('click', function() { 
     walletName = $('#addressName').val(); 

     // prints the value 
     console.log(walletName); 
    }) 

    // doesn't print the value 
    console.log("Wallet = " + walletName); 
}); 

console.log(walletName);實際上打印錢包的名字,但是,當我出門時,console.log("Wallet = " + walletName);打印Wallet = undefined

我認爲walletName;是全球性的,因此它也應該在控制檯中輸出值undefined。這裏有什麼問題?

更新:

把變量out範圍後,我仍然有同樣的問題,

var walletName; 

$(document).ready(function() { 

    var selectedCurrency; 

    // some code 
}); 

enter image description here

回答

2

更改您的代碼

var walletName; 
$(document).ready(function() { 
    var selectedCurrency; 
相關問題