javascript - 在 JavaScript 的全局上下文中使用输入值

标签 javascript jquery html

我使用一个小应用程序,前端看起来如下所示,

enter image description here

选择货币后,Address Generate 按钮处于事件状态,单击它会打开一个带有输入选项的模式。

enter image description here

我想在全局上下文中使用钱包名称。着陆页的代码在这里,

<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。这里有什么问题?

更新:

将变量移出范围后,我仍然有同样的问题,

var walletName;

$(document).ready(function () {

    var selectedCurrency;

    // some code 
});

enter image description here

最佳答案

将您的代码更改为

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

关于javascript - 在 JavaScript 的全局上下文中使用输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45706395/

相关文章:

javascript - 使用 "months"数组在 for 循环中执行每个月的计算

php - jquery-ajax : pass values from a textfield to php file and show the value

html - 在页面上定位图像 (HTML)

jquery - 通过悬停在 materializecss 中的激活器来打开卡片显示内容

javascript - 删除JS数组中的最小数字

javascript - 以编程方式获取无线接入点列表

即使使用比较函数,Javascript 按数字总和排序也不起作用。为什么?

javascript - 提交多个表单同一个 jquery 脚本

wcf - 将 JSON 发送到 WCF Rest 服务 - 对象始终为 null

java - 当页面需要登录时,如何用Java下载HTML源代码?