【问题标题】:Unable to update global variable using javascript on button click无法在按钮单击时使用 javascript 更新全局变量
【发布时间】:2019-01-11 14:48:04
【问题描述】:

我需要更新price 全局变量。我相信这可能与范围有关。如果您能在这方面提供帮助,我将不胜感激。

这是脚本:

var price = 0;

var nextdayClass = $('.delivery1');
var $standardClass = $('.delivery2');
var $pickupClass = $('.delivery3');

nextdayClass.on('click', function() {
               var nextday = $('#nextday').data('price');
               price = nextday;
               console.log(price);
});
standardClass.on('click', function () {
                var standard = $('#standard').data('price');
                price = standard;
                console.log(price);
            });
pickupClass.on('click', function () {
                var pickup = $('#pickup').data('price');
                price= pickup;
                console.log(price);
            });
console.log(price);

cartTotalHTML += '<div>' +
                '<ul>' +
                '<li>' +
                '<div>Subtotal</div>' +
                '<div>' + formatMoney(total) + '</div>' +
                '</li>' +
                '<li>' +
                '<div>Shipping</div>' +
                '<div>' + formatMoney(price) + '</div>' +
                '</li>' +
                '<li>' +
                '<div>Total</div>' +
                '<div>' + totalAfterShipping(total, price) + '</div' +
                '</li>' +
                '</ul>' +
                '</div>';
$('#cartOutput').html(cartItemHTML);

这是我从中获取数据的 html:

                <div class="delivery">
                            <div>Shipping method</div>
                            <div>Select the one you want</div>
                            <div class="delivery_options">
                                <label>Next day delivery
                                    <input id="nextday" type="radio" name="radio" data-name="nextday" data-price="9000">
                                    <span class="checkmark delivery1"></span>
                                    <span class="delivery_price">R90</span>
                                </label>
                                <label>Standard delivery
                                    <input id="standard" type="radio" name="radio" data-name="standard" data-price="3000">
                                    <span class="checkmark delivery2"></span>
                                    <span >R30</span>
                                </label>
                                <label>Personal pickup
                                    <input id="pickup" type="radio" checked="checked" data-name="pickup" data-price="0" name="radio">
                                    <span class="checkmark delivery3"></span>
                                    <span >Free</span>
                                </label>
                            </div>
                        </div>

这是我将数据带到的 html:

<div class="col-lg-6 offset-lg-2">
                        <div class="cart_total">
                            <div>Cart total</div>
                            <div>Final info</div>
                            <div id="cartTotalOutput">

                            </div>
                            <div><input type="submit" class="button checkout_button"></div>
                        </div>
                    </div>
                </div>

【问题讨论】:

  • 您的问题/问题到底是什么?

标签: javascript jquery variables scope global


【解决方案1】:

这里有两个问题。首先add() 是将元素添加到集合中,而不是附加事件处理程序。做你想做的事,使用click()on()

其次,price 仅在点击事件发生后 更新,但您的逻辑试图立即读取它。要解决此问题,您需要将console.log() 行放入该事件处理程序中。试试这个:

var price = 0;
var $nextdayClass = $('.delivery1');

$nextdayClass.on('click', function() {
  var nextday = $('#nextday').data('price');
  price = nextday;
  console.log(price);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="delivery1">Click me</button>

<div id="nextday" data-price="1.99">Next day: 1.99</div>

还值得注意的是,您应该尽可能避免使用全局变量。更好的使用模式是检索 data 属性,该属性包含实际需要的价格,并完全删除 price 变量。

【讨论】:

  • 您好,Rori,非常感谢您的回答。我需要在函数修改价格变量后使用它,这就是我将 console.log() 放在函数之外的原因。
  • 您仍然不应该为此使用全局变量。不过,如果没有看到您的全部逻辑在做什么,我真的无法提供任何替代方案
  • 您好,我已经用所有逻辑更新了我的帖子,我相信它现在应该更有意义了。
【解决方案2】:

它与范围无关。

看看你的代码:

  1. 你得到一个元素
  2. 你说你点击元素price应该被更新(好吧,你尝试到,你打错了,叫add而不是@ 987654323@)
  3. 你看price

大概在稍后的某个时间点,您会单击该元素。

此时price 已更新。

你不要再看它了。

JavaScript 不会穿越到过去并在您第一次查看之前更改 price

控制台中显示的查看时的值记录不会改变。

如果要在单击元素后记录值,则必须将执行记录的代码放入单击元素时调用的函数中。

var price = 0;
var nextdayClass = $('.delivery1');

nextdayClass.on('click', function() {
  var nextday = $('#nextday').data('price');
  price = nextday; // The window object stuff is a waste of time
  console.log("Clicked value", price);

});
console.log("Initial value", price);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="delivery1">5</button>
<span id=nextday data-price=5></span>

【讨论】:

  • 嗨昆汀,我希望初始值为 5 而不是 0
  • @PG1728 — 所以在第一行设置为 5。但这会使按钮毫无意义。
  • 对不起,我们可能有误解,我希望代码的输出是:初始值:5 点击值:5
  • @PG1728 — 所以在第一行设置为 5。如果初始值为 5,那么它会说初始值为 5。您仍然无法使 JavaScript 时间旅行,因此如果单击按钮,那么单击按钮之前的值会有所不同。
  • @PG1728 — 看起来同样的问题。在您最初的问题中,当您 console.loged 时,您得到了错误的值,因为您在错误的时间查看它。解决方案是查看单击处理程序中的值。在您的更新中,您在 HTML 中得到了错误的值,因为您在错误的时间更新了 HTML。从您的点击处理函数更新 HTML。
【解决方案3】:

像这样尝试JQuery on(),同样我假设您的元素可能是动态生成的,尝试将事件处理程序绑定到body元素/

var price = 0;
var nextdayClass = $('.delivery1');

$('body').on('click', nextdayClass, function() {
  var nextday = $('#nextday').data('price');
  window['price'] = nextday;
});
console.log(price); //Prints out 0

【讨论】:

  • Downvote 不是我的,但请注意,您还没有解决访问全局变量的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-18
  • 1970-01-01
相关资源
最近更新 更多