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

jQuery实现勾选复选框后禁用值包含其拆分关键词的其他复选框

调整后完整代码
$(document).ready(function() {
  $('input[id^="takaritas"]').click(function() {
    if ($("#kategoria").val() == "") {
      $('input[id^="takaritas"]').prop("checked", false);
      $('input[id^="takaritas"]').val("");
      alert("Válasszon autó kategóriát!");
    } else if ($("#takaritasnincs").is(":checked")) {
      $('input[id^="takaritas"]').not(this).prop("disabled", true);
      $('input[id^="takaritas"]').not(this).prop("checked", false);
      $('input[id^="takaritaskoltseg"]').val("");
    } else if ($(this).not("#takaritasnincs").is(":checked")) {
      // 提取并清洗有效关键词
      let rawArray = $(this).val().split(" ");
      const filteredKeywords = rawArray
        .filter(item => item.trim() !== '+')
        // 全局移除mosás、polír,不区分大小写
        .map(item => item.replace(/mosás|polír/gi, '').trim())
        // 过滤空值避免无效匹配
        .filter(item => item.length > 0);
      
      // 遍历所有其他复选框做匹配
      $('input[id^="takaritas"]').not(this).not('#takaritasnincs').each(function() {
        const currentCheckboxVal = $(this).val();
        // 检查当前复选框值是否包含任意有效关键词
        const needDisable = filteredKeywords.some(keyword => {
          // 若需要不区分大小写匹配,可改为下行注释内容
          // return currentCheckboxVal.toLowerCase().includes(keyword.toLowerCase())
          return currentCheckboxVal.includes(keyword);
        });
        if (needDisable) {
          $(this).prop("disabled", true).prop("checked", false);
        }
      });
    } else {
      // 取消勾选时恢复所有复选框可用
      $('input[id^="takaritas"]').prop("disabled", false);
    }
  });
});

核心调整说明

  • 关键词提取阶段先过滤加号,再统一清洗掉mosás、polír两类无效片段,同时过滤空值,避免无效匹配
  • 匹配逻辑从原来的「value精确等于关键词」改为「value包含任意关键词即命中」,符合要求的匹配规则
  • 自动跳过当前勾选的复选框本身,不会出现勾选后把自己禁用的问题
  • 统一使用prop方法操作DOM属性,替换原来混用attr的写法,兼容jQuery 3.x的属性操作规范

内容的提问来源于stack exchange,提问作者Daniel H.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 14:36:01