18910140161

JavaScript-jQuery值包括数组堆栈溢出

顺晟科技

2022-10-19 13:02:06

14

我根据一个有用的答案更新了这个问题,这使我更加接近,但遗憾的是没有完全达到我需要的目的。

更新:

我正在尝试创建一个jQuery函数,该函数将禁用任何与当前选中的复选框值的任何部分匹配的复选框。

单击以“Külsó+Belsó+Wax+Motormosás”为值的复选框,将返回以下数组:

$(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).attr("disabled", "disabled");
      $('input[id^="takaritas"]').not(this).prop("checked", false);
      $('input[id^="takaritaskoltseg"]').val("");
    } else if ($(this).not("#takaritasnincs").is(":checked")) {

      let array = $(this).val().split(" ");
      array.forEach((el, index) => {
        array[index] = array[index].replace("mosás", "").replace("Polír", "");
      });
      const filteredArray = array.filter((x) => x !== "+");
      filteredArray.forEach((el) => {

        let input = document.querySelector('[value="' + el + '"]');
        if (input) {
          document.querySelector(
            'input[type="checkbox"][value="' + el + '"]'
          ).disabled = true;
          document.querySelector(
            'input[type="checkbox"][value="' + el + '"]'
          ).checked = false;
        }
      });
    } else {
      $('input[id^="takaritas"]').removeAttr("disabled");
    }
  });
});

我需要这个函数来禁用包含数组任何部分的复选框,忽略加号和部分字符串“mosás”和“polír”。

不胜感激。


顺晟科技:

如果存在该数组,则可以禁用该数组:


更新后编辑: 您需要使用带有通配符的querySelectorAll来匹配所有值中的一个数组元素,然后foreach并禁用它。

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航