【问题标题】:Changing Woocommerce product variation with jQuery after click on autonomous button单击自主按钮后使用 jQuery 更改 Woocommerce 产品变体
【发布时间】:2019-07-04 01:17:20
【问题描述】:

我在 Woocommerce 的产品页面上有一个弹出窗口,其中包含变体图片和每个变体的单独按钮。我想在用户单击特定按钮后更改变化。

我已尝试将 jQuery 添加到某个选项,但它不会更新变体数据,只会更新选择输入。

当我在使用经典选择更改变体后在 DevTools 中观看表单时,表单本身没有任何变化。

我也尝试过使用 jQuery 更改 Select 值,但它根本不起作用:

jQuery('#choose-berlin').click(function() {

        jQuery('#city').val("Berlin").change();

    });

选择结构:

<select id="city" class="" name="attribute_city" data-attribute_name="attribute_city" data-show_option_none="yes">
<option value="">Choose option</option>
<option value="Berlin" class="attached enabled">Berlin</option>
<option value="London" class="attached enabled">London</option>
</select>

弹出窗口中的一个按钮:

<a href="#" role="button" id="choose-berlin">
    Choose Berlin
</a>

如何正确触发变异变化?

【问题讨论】:

    标签: jquery wordpress woocommerce


    【解决方案1】:

    试试这个:

    jQuery('#choose-berlin').click(function() {
            jQuery('#city').val("Berlin").trigger('change');
    });
    

    这假定您已将城市添加为 .variations_form 表单中的属性。

    Woocommerce 中有很多基础功能,其中的变体字段在change 和其他几个自定义事件上具有动作侦听器。如果使用 JS 更改选择字段,则需要确保在选择上触发更改操作。

    【讨论】:

    • 感谢您的回答。不幸的是,什么也没有发生。是的,它在data-product_variations='[{"attributes" 中以.variations_form 形式列在其他变体中,并作为select #city 中的选项列出。
    猜你喜欢
    • 1970-01-01
    • 2022-09-26
    • 2019-07-16
    • 2016-10-02
    • 2021-06-03
    • 2019-07-16
    • 2017-04-11
    • 2016-08-14
    • 1970-01-01
    相关资源
    最近更新 更多