You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

jQuery实现点击时隔离子元素行为 仅选中单个产品对应选项

问题根因

你现有代码的问题是使用了全局选择器匹配所有同值复选框,没有限定操作范围,才会出现跨产品联动的问题。

解决思路

点击复选框后,先定位到当前产品的父容器,后续所有DOM操作都只在该父容器内执行,就能实现产品间的操作隔离。

修改后代码

$('[name="subscribe"], [name="not_subscribe"]').off('click').on('click', function (event) {
    // 此处替换为你页面中每个产品模块实际的父容器选择器即可
    const $currentProduct = $(this).closest('.product-item');
    const isSubscribe = $(this).val() === 'subscribe';
    const isChecked = $(this).prop('checked');

    if (isChecked) {
      // 所有查询都限定在当前产品容器内,不会影响其他产品
      $currentProduct.find('input[type="checkbox"][value="not_subscribe"]').prop('checked', !isSubscribe);
      $currentProduct.find('input[type="checkbox"][value="subscribe"]').prop('checked', isSubscribe);
      $currentProduct.find('[data-amrecurring="recurring-settings"]').toggle(isSubscribe);
    } else {
      $currentProduct.find('input[type="checkbox"][value="not_subscribe"]').prop('checked', isSubscribe);
      $currentProduct.find('input[type="checkbox"][value="subscribe"]').prop('checked', !isSubscribe);
      $currentProduct.find('[data-amrecurring="recurring-settings"]').toggle(!isSubscribe);
    }
});

补充说明

你可以打开浏览器开发者工具,查看页面中每个产品模块最外层包裹元素的类名,将代码里的.product-item替换为对应的实际选择器即可正常运行。如果你的场景不需要支持取消勾选,换成原生互斥的单选框(type="radio")会更合理,无需手动控制勾选状态。

内容的提问来源于stack exchange,提问作者BartZalas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 13:00:01