【问题标题】:unable to get onchange() to work in woocommerce无法让 onchange() 在 woocommerce 中工作
【发布时间】:2015-05-10 13:06:37
【问题描述】:

我正在尝试检测 woocommerce 结帐字段中帐单国家/地区下拉框中的值是否已更改。如果发生了变化,则获取用户选择的国家的值,然后对其进行处理。

为此,我使用如下 jquery 代码:

    $('#billing_country').on('change',function() {
        alert(this.value);
        // do something like alerting users that the book can not be shipped to the selected country.
    });

以下是我在 woocommerce 的结帐表单页面中找到的 HTML 代码。

    <select id="billing_country" class="country_to_state country_select " name="billing_country" tabindex="-1" title="Country *" style="display: none;">
    <option value="">Select a country…</option>
    <option value="AX">Åland Islands</option>
    <option value="AF">Afghanistan</option>
    <option value="AL">Albania</option> 
    ...

上面代码的问题是,当我从下拉框中选择一个不同的国家时,什么都没有发生。

非常感谢您

【问题讨论】:

  • style="display: none;"这是什么意思?
  • 你用的是哪个版本的jquery?

标签: jquery wordpress woocommerce


【解决方案1】:

查看此回复:Run javascript code on billing country change in WooCommerce checkout

您需要将您的事件委托给文档正文,否则 WooCommerce 将在结帐时阻止您的代码。

由于 WooCommerce JS 代码已经使用 jQuery,请尝试以下操作:

jQuery(function($){
    $(document.body).on('change', 'select[name=billing_country]', function(){
        console.log('Country changed: '+$(this).val());
        // Here run your function or code
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 2018-02-26
    相关资源
    最近更新 更多