Woocommerce中的产品附加组件Checkbox Max 2

问题描述 投票:1回答:2

使用WooCommerce Product Add-Ons插件我试图控制复选框,不允许选中2个以上的复选框。这不是插件可用的选项。

我该怎么做到这一点?

我的实际代码:

<script>
    jQuery(function ($) { 
        var $checkboxes = $('.addon-checkbox').click(function () {
            if ($checkboxes.filter(':checked').length > 2) {   
                $(this).prop('checked', false); return false;
            } 
        });
    });
</script> 
wordpress checkbox woocommerce
2个回答
1
投票

这可能不是最好或最干净的方式,但你可以使用jQuery来limit the checkbox selections。您需要更改选择器以定位复选框上的任何类。

(function($){
    var limit = 2;
    $('input.option-checkbox').on('change', function(event) {
        if($(this).siblings(':checked').length >= limit) {
            this.checked = false;
        }
    });
}(jQuery))

将此脚本保存到文件中,然后在functions.php文件中将其排除。

wp_enqueue_script( 'custom-checkboxes', get_stylesheet_directory_uri() . '/assets/js/custom-checkboxes.js', array('jquery'), '1.0.0', true);

1
投票

我试过这个,它完美无缺!

<script>
jQuery(function ($) {
    var $checkboxes = $('.addon-checkbox').click(function () {
        if ($checkboxes.filter(':checked').length > 2) {
            $(this).prop('checked', false);
            return false;
        }
    });
});</script>

.addon的课不适合我。

© www.soinside.com 2019 - 2024. All rights reserved.