html - 使用 jQuery 更改链接点击上的 Woocommerce 产品数量

标签 html jquery wordpress woocommerce product-quantity

在产品详细信息页面上,我希望有几个按钮来允许用户选择特定数量。例如150、300、500。单击时,数量输入字段应更新为用户单击的值。

我尝试了一个简单的点击功能,但它根本没有触发 - 控制台中也没有任何内容。

<a id="quantity150">150</a>

$('#quantity150').click(function () {
      $('[name="quantity"]').val('150');
      console.log('#quantity150 was clicked');
    });

Woocommerce 中的数量输入字段的名称=“数量”

可以在此处查看此功能的示例:https://www.anypromo.com/trade-show-events/printed-cards/plastic-wallet-card-p730694 (数量表-点击后会改变数量输入栏)

最佳答案

在 WordPress 中,您需要将 $ 别名替换为 jQuery。请改用以下内容:

<a id="quantity150" class="button" href="#">150</a>
<script>
jQuery( function($) {
    $('#quantity150').click( function(e) {
        e.preventDefault();
        $('[name="quantity"]').val('150');
        console.log('#quantity150 was clicked');
    });
});
</script>

在 WooCommerce 单一产品页面上测试了作品。

关于html - 使用 jQuery 更改链接点击上的 Woocommerce 产品数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66480006/

相关文章:

javascript - 如何让一个 div 随着窗口大小的调整而移动,而不会将其他 div 推开?

html - wordpress 是如何获得这种微妙的 3d 效果的?

html - 我不知道为什么 Flexbox 元素会拉伸(stretch)

javascript - 如何将我的下拉 CSS 菜单栏放在我的 jQuery 图片旋转器的顶部?

javascript - 当元素到达某个点时更改 CSS 属性

jQuery 突然停止工作

javascript - 确定哪个 div 在 div 列表的中间

php - 在循环中显示自定义帖子类型(专门针对该帖子)的 WordPress 类别

jquery - Zurb Foundation 顶部栏菜单无法在移动显示屏上运行

jquery - 我想使用 custome css 更改我的 wordpress 网站的大型菜单中 Font Awesome 图标的大小