2017-08-16 1 views
1

ich mit einer kleinen App arbeiten und das Front-End sieht wie folgt aus,Verwenden Sie den Eingabewert in globalem Kontext in JavaScript

enter image description here

Nachdem ich die Währung wählen, die Address Generate Taste aktiv ist und mit Klick Es öffnet eine Modal mit Eingabeoption.

enter image description here

Ich möchte die wallet name im globalen Kontext haben. Der Code für die Zielseite ist hier,

<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> 

Die Projektstruktur folgt,

enter image description here

Die JavaScript in der main.js Datei geschrieben wird,

$(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); 
}); 

Die console.log(walletName); tatsächlich druckt den Namen der Brieftasche, aber wenn ich ausgehe, druckt die console.log("Wallet = " + walletName);Wallet = undefined .

Ich denke, die walletName; ist global und daher sollte es auch den Wert dann die undefined in der Konsole drucken. Was ist das Problem hier?

Update:

Nachdem die Variable aus dem Rahmen setzen, habe ich noch das gleiche Problem haben,

var walletName; 

$(document).ready(function() { 

    var selectedCurrency; 

    // some code 
}); 

enter image description here

Antwort

2

Ändern Sie Ihren Code

var walletName; 
$(document).ready(function() { 
    var selectedCurrency; 
Verwandte Themen